Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:31:03+00:00 2026-06-15T13:31:03+00:00

Can anyone help me compare an Integer to a Double using generics? This is

  • 0

Can anyone help me compare an Integer to a Double using generics?

This is what I have:

public static <T extends Comparable<? super T>> int compare(T arg1, T arg2)
{
    return arg1.compareTo(arg2);
}

public static void main(String[] args)
{   
    Number i = new Integer(5);
    Number j = new Double(7);

    System.out.println(GenericsTest.compare(i, j));
}

The error message I get is:
Bound mismatch: The generic method compare(T, T) of type GenericsTest is not applicable for the arguments (Number, Number). The inferred type Number is not a valid substitute for the bounded parameter >

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:31:04+00:00Added an answer on June 15, 2026 at 1:31 pm

    The idea of this solution is to widen to BigDecimal and then compare the two numbers (now is cleaner but somehow formatting doesn’t work). Note you may reuse this static comparator without having to cast to double anywhere else. In the implementation you do need conversion to double not to lose information, basically you widen to the most general representation.

    private static final Comparator<Number> NUMBER_COMPARATOR = new Comparator<Number>() {
        private BigDecimal createBigDecimal(Number value) {
            BigDecimal result = null;
            if (value instanceof Short) {
                result = BigDecimal.valueOf(value.shortValue());
            } else 
            if (value instanceof Long) {
                result = BigDecimal.valueOf(value.longValue());             
            } else 
            if (value instanceof Float) {
                result = BigDecimal.valueOf(value.floatValue());                                
            } else 
            if (value instanceof Double) {
                result = BigDecimal.valueOf(value.doubleValue());                               
            } else 
            if (value instanceof Integer) {
                result = BigDecimal.valueOf(value.intValue());                              
            } else {
                throw new IllegalArgumentException("unsupported Number subtype: " + value.getClass().getName());
            }
                           assert(result != null);
    
            return result;
        }
    
        public int compare(Number o1, Number o2) {
            return createBigDecimal(o1).compareTo(createBigDecimal(o2));
        };
    };
    
    public static void main(String[] args) {
        Number i = Integer.valueOf(5);
        Number j = Double.valueOf(7);
                  // -1
        System.out.println(NUMBER_COMPARATOR.compare(i, j));
    
             i = Long.MAX_VALUE;
             j = Long.valueOf(7);
                  // +1
             System.out.println(NUMBER_COMPARATOR.compare(i, j));
    
             i = Long.MAX_VALUE;
             j = Long.valueOf(-7);
                  // +1
             System.out.println(NUMBER_COMPARATOR.compare(i, j));
    
             i = Long.MAX_VALUE;
             j = Double.MAX_VALUE;
                  // -1
             System.out.println(NUMBER_COMPARATOR.compare(i, j));
    
        i = Long.MAX_VALUE;
        j = Long.valueOf(Long.MAX_VALUE - 1);
        // +1
        System.out.println(NUMBER_COMPARATOR.compare(i, j));
    
                  // sorting Long values
        Long[] values = new Long[] {Long.valueOf(10), Long.valueOf(-1), Long.valueOf(4)};
        Arrays.sort(values, NUMBER_COMPARATOR);
                  // [-1, 4, 10] 
        System.out.println(Arrays.toString(values));  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help - this is driving me mad. I am calling a mysql
Can Anyone help me why x2 prints zero. I guess because of floating point
Can anyone help me how to split /explode verse format to 3 parts? The
Can anyone help me to get good WordPress interview questions and answers. Any link
Can anyone help me in converting scalar type of openCV to basic types like
can anyone help me for the exercise 12.5 of Jason Hickey's book? Basically, the
Can anyone help me with the script to refresh the page once . I
Can anyone help me find an up-to-date, working ATL project which has a main
Can anyone help or provide me with some suggestions for the below query. I
Can anyone help me understand what is going on here? Jenkins has been working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.