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 8766671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:36:45+00:00 2026-06-13T16:36:45+00:00

I am using some code with generics in which I am unsure why there

  • 0

I am using some code with generics in which I am unsure why there is a compile error being thrown.

  public static void main(String[] args) {

    System.out.print("s");
    setInteger(prepareNumber("1")); // Error here! solved by casting: setInteger((Integer)prepareNumber("1"))
    Integer c = prepareInteger("1");
    System.out.print(c);

  }

  public static Integer prepareInteger(Object number) {
    return prepareNumber(number).intValue();
  }

  private static <T extends Number> T prepareNumber(Object number) {

    T returnValue = null;

    // handle a blank number
    if (number == null || !NumberUtils.isNumber(number.toString())) {
      returnValue = null;
    } else {
      if (number.toString().contains(".")) {
        returnValue = (T) Double.valueOf(number.toString());
      } else {
        returnValue = (T) Integer.valueOf(number.toString());
      }
    }

    return returnValue;
  }

  private static void setInteger(Integer a){    
  }

Fixing this is easy by just doing a casting, but that is undesired, as the generics should detect the method needs an Integer and should provide the return value as such and not as the super type.

PS: Do not mind the parsing code, is just testing stuff.

Thanks!

  • 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-13T16:36:46+00:00Added an answer on June 13, 2026 at 4:36 pm

    The compiler is unable to infer what type prepareNumber("1") is supposed to return in this case. You can get it to infer correctly by placing the result in a temporary variable:

    Integer value = prepareNumber("1");        
    setInteger(value);
    

    Or you if parseNumber is a static method in class YourClass you can explicitly tell the compiler what type you expect like so:

    setInteger(YourClass.<Integer>prepareNumber("1"));
    

    Or if you are okay with changing the method signature to help the compiler:

    setInteger(prepareNumber("1", Integer.class));
    
    private static <T extends Number> T prepareNumber(Object number, Class<T> classType) {
       ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's an interesting problem. Is there a way to write some code using LINQ
I have some Java code that needs to compile with current generics aware compilers
I have some work code whereby using generics has started to make the code
I'm using some code from here to determine when determining when the last finger
I'm using some code to build up tables using JQuery, but in Firefox 3.5.3
I'm trying to parse a html doc using some code I found from this
I am using some legacy code to return an Excel worksheet as a Dataset.
When my application first runs I'm using some simple code to read in some
I was able to successfully run an RF model using some R code I
i have some code using jquery mobile + phonegap and run it on my

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.