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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:04:26+00:00 2026-05-23T12:04:26+00:00

Given the generic method: <T> List<T> getGenericList(int i) {…} the following code compiles without

  • 0

Given the generic method:

<T> List<T> getGenericList(int i) {...}

the following code compiles without any warning:

public List<String> getStringList(boolean b){
    if(b)
        return getGenericList(0);
    else
        return getGenericList(1);
}

but this one generates ‘Type mismatch’ compilation error:

public List<String> getStringList(boolean b) {
    return (b) ? getGenericList(0) : getGenericList(1);
}

Why?

  • 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-05-23T12:04:27+00:00Added an answer on May 23, 2026 at 12:04 pm

    This is NOT a generics problem, but a consequence of the way the compiler has to infer the type of the ternary expression.

    It happens the same with this equivalent code. This code works:

    public byte function(boolean b){
        if(b)
            return 1;
        else
            return 2;
    }
    

    While this doesn’t:

    public byte function(boolean b) {
        return (b) ? 1 : 2;
    }
    

    The reason is that when the compiler tries infer the type of this expression

        return (b) ? 1 : 2;
    

    It first has to obtain the type of each one of the operands, and check if they are compatible (reference) to evaluate wether the ternary expression is valid or not. If the type of the “return” were propagated to automatically cast or promote each one of the operands, it could lead to resolve the type of a ternary expression differently depending on the context of this expression.

    Given that the type of the “return” cannot be propagated to the operands, then in the menctioned case:

        return (b) ? getGenericList(0) : getGenericList(1);
    

    the binding of the generic type cannot be done, so the type of each one of the operands is resolved to List<Object>. Then the compiler concludes that the type of the whole expression is List<Object>, which cannot be automatically casted to List<Integer> (because they are not compatible types).

    Whereas this other one

        return getGenericList(0);
    

    It applyes the type of the “return” to bind the generic type T, so the compiler concludes that the expression has a List<String> type, that can be returned safely.

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

Sidebar

Related Questions

Given a generic class: class MyClass ( private List l = new LinkedList <String>();
Given a Generic List of objects that contain a member variable that is a
I've got a System.Generic.Collections.List(Of MyCustomClass) type object. Given integer varaibles pagesize and pagenumber, how
I'd like to create a data table given a List using the CopyToDataTable method
Is it a code smell to have to following pattern, given the following code
Given a Python object of any kind, is there an easy way to get
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a select with multiple option's in jQuery. $select = $(<select></select>); $select.append(<option>Jason</option>) //Key =
Given a DateTime representing a person's birthday, how do I calculate their age in

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.