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

  • Home
  • SEARCH
  • 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 957495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:42:41+00:00 2026-05-16T00:42:41+00:00

Assigning values without using usual notation like this.<Double>getAnything(int flag) private <T> T getAnything(int flag)

  • 0

Assigning values without using usual notation like “this.<Double>getAnything(int flag)”

private <T> T getAnything(int flag) {
    Object o = null;
    if (flag==0)
        o=new String("NewString");
    else if (flag==1)
        o=new Double(0D);
    return (T)o;
}


private void someMethod() {
    String s = getAnything(0);
    Double d = getAnything(1);
}

in the past it was enough only a return object on the method and a simple cast onthe receiveing type, so with the lacking of generic notation on the receiver object it is much more similar and fast to write, any other hint on this?

  • 1 1 Answer
  • 3 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-05-16T00:42:42+00:00Added an answer on May 16, 2026 at 12:42 am

    I think you’re confusing autoboxing with type inference.

    Type inference is when the compiler can tell what type it should use on a generic method on its own, from the variables used when calling the method.

    For example if you have the following method:

    public <T extends SomeClass> T process(T obj) {
        // call some methods of SomeClass on obj to process it here
        return obj;
    }
    

    and then call it like:

    SomeChildClass a = new SomeChildClass(); // SomeChildClass extends SomeClass
    a = process(a);
    

    the inferred type will be SomeChildClass;

    The type can be inferred from the parameters or from the return type, as is in your example. But it’s not always obvious to the compiler what type it should use. If that happens you can force the type by using the method this.<Double>getAnything(int flag) that you described. This usually happens in situations like this:

    public <T> List<T> getSomeList() {
        // implementation
    }
    
    public void processList(List<SomeClass> list) {
         // implementation
    }
    

    and calling

    processList(getSomeList()); // compiler error: cannot convert List<Object> to List<SomeClass>
    

    In cases like this you may need to force the type parameter.

    All this being said, please take into consideration everything that polygenelubricants said as well, as he makes some very good points regarding your code and explains what autoboxing is (it’s related to primitive wrapper classes like Integer for int, and Double for double).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Explicitly assigning values to a 2D Array? sorry if this has been
Any way to declare a new variable in F# without assigning a value to
I am getting Out of memory exception when assigning values in session variables. The
I'm new to OOP. Originally I was defining variables and assigning values to them
How to use prototype instead of innerHTML while assigning the values to the elements
Possible Duplicate: Unsigned long with negative value Assigning negative numbers to an unsigned int?
I am having difficulties in assigning double value (for example 124.00) of label text
I would like to create one class which holds some key values of the
I am trying to select specific values from a xml document using XPath. The
I am trying to select specific values from a xml document using XPath. The

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.