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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:02:29+00:00 2026-05-23T15:02:29+00:00

I don’t think I really understand Java generics. What’s the difference between these two

  • 0

I don’t think I really understand Java generics. What’s the difference between these two methods? And why does the second not compile, with the error shown below.

Thanks

static List<Integer> add2 (List<Integer> lst) throws Exception {
    List<Integer> res = lst.getClass().newInstance();
    for (Integer i : lst) res.add(i + 2);
    return res;
}

.

static <T extends List<Integer>> T add2 (T lst) throws Exception {
    T res = lst.getClass().newInstance();
    for (Integer i : lst) res.add(i + 2);
    return res;
}

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
  required: T
  found:    capture#1 of ? extends java.util.List
  • 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-23T15:02:30+00:00Added an answer on May 23, 2026 at 3:02 pm

    For the second method to compile, you have to cast the result of newInstace() to T:

    static <T extends List<Integer>> T add2 (T lst) throws Exception {
      T res = (T) lst.getClass().newInstance();
      for (Integer i : lst) res.add(i + 2);
      return res;
    }
    

    Regarding the difference between the two methods, let’s forget about the implementation, and consider only the signature.

    After the code is compiled, both methods will have exactly the same signature (so the compiler would give an error if the have the same name). This happens because of what is called type erasure.

    In Java, all the type parameters disappear after compilation. They are replaced by the most generic possible raw type. In this case, both methods will be compiled as List add2(List).

    Now, this will show the difference between the two methods:

    class Main {
      static <T extends List<Integer>> T add1(T lst) { ... }
      static List<Integer> add2(List<Integer> lst) { ... }
      public static void main(String[] args) {
        ArrayList<Integer> l = new ArrayList<Integer>();
        ArrayList<Integer> l1 = add1(l);
        ArrayList<Integer> l2 = add2(l); // ERROR!
      }
    }
    

    The line marked as // ERROR! won’t compile.

    In the first method, add1, the compiler knows that it can assign the result to a variable of type ArrayList<Integer>, because the signature states that the return type of the method is exactly the same as that of the parameter. Since the parameter is of type ArrayList<Integer>, the compiler will infer T to be ArrayList<Integer>, which will allow you to assign the result to an ArrayList<Integer>.

    In the second method, all the compiler knows is that it will return an instance of List<Integer>. It cannot be sure that it will be an ArrayList<Integer>, so you have to make an explicit cast, ArrayList<Integer> l2 = (ArrayList<Integer>) add2(l);. Note that this won’t solve the problem: you are simply telling the compiler to stop whining and compile the code. You will still get an warning (unchecked cast), which can be silenced by annotating the method with @SuppressWarnings("unchecked"). Now the compiler will be quiet, but you might still get a ClassCastException at runtime!

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

Sidebar

Related Questions

I don’t think I’ve grokked currying yet. I understand what it does, and how
Don't these two mean the same thing, first get the value and then increment?
Don't think that I'm mad, I understand how php works! That being said. I
Don't know the difference between the System.Window.Controls.TextBox and System.Windows.Forms.TextBox . Noticed that the System.Windows.Forms.TextBox
Don't want to sort the entries. using this does not preserve the order as
Don't understand why #include <Header.h> is not compiling while #include Header.h is compiling with
Don't think this is a repost, difficult to search for the word between because
Don't let below code scare you away . The question is really simple, only
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
Don't really know how to formulate the title, but it should be pretty obvious

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.