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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:48:34+00:00 2026-05-27T21:48:34+00:00

The following code class GenericCompilationFailureDemo { List<? extends GenericCompilationFailureDemo> newList() { return new ArrayList<GenericCompilationFailureDemo>();

  • 0

The following code

class GenericCompilationFailureDemo {
    List<? extends GenericCompilationFailureDemo> newList() { 
        return new ArrayList<GenericCompilationFailureDemo>(); 
    };

    void useList() {
        List<GenericCompilationFailureDemo> list = 
            (List<GenericCompilationFailureDemo>) newList();
    }  

    List<? extends Set<GenericCompilationFailureDemo>> newListOfSpecificSets() { 
        return new ArrayList<Set<GenericCompilationFailureDemo>>(); 
    };

    void useListOfSpecificSets() {
        List<Set<GenericCompilationFailureDemo>> listOfSpecificSets = 
            (List<Set<GenericCompilationFailureDemo>>) newListOfSpecificSets();
    } 

    List<? extends Set<? extends GenericCompilationFailureDemo>> newListOfSets() { 
        return new ArrayList<Set<? extends GenericCompilationFailureDemo>>(); 
    };

    void useListOfSet() {
        List<Set<? extends GenericCompilationFailureDemo>> listOfSets = 
            (List<Set<? extends GenericCompilationFailureDemo>>) newListOfSets();
    }  
}

compiles under Sun JDK 1.6.0_20 (64-bit on Windows Vista, but I don’t think that makes any difference) but causes the following compilation failure under Oracle JDK 1.7.0_01 (same platform):

[ERROR] src\main\java\GenericCompilationFailureDemo.java:[56,78] error: inconvertible types

Note that the first two “extends-to-specific-type” casts in useList and useListOfSpecificSets both still succeed under 1.7.0_01, so it would seem it’s something to do with the “double generic extends”.

Any ideas what might have changed between 6 and 7, and whether the observed behaviour is according to spec or a bug?

edited in response to Sanjay’s comment:

@Sanjay: Aha, interesting! Here the output from java -version:

java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) 64-Bit Server VM (build 21.1-b02, mixed mode)

And here the result of javac GenericCompilationFailureDemo.java (same code as above with import statements for List, ArrayList and Set):

GenericCompilationFailureDemo.java:30: error: inconvertible types
            (List<Set<? extends GenericCompilationFailureDemo>>) newListOfSets()
;
                                                                              ^
  required: List<Set<? extends GenericCompilationFailureDemo>>
  found:    List<CAP#1>
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Set<? extends GenericCompilationFailureDemo> from capture of ?
 extends Set<? extends GenericCompilationFailureDemo>
Note: GenericCompilationFailureDemo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
  • 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-27T21:48:34+00:00Added an answer on May 27, 2026 at 9:48 pm

    This is apparently a javac7 bug. It should be allowed per casting conversion rules [1]

    One of the rules allows a narrowing reference conversion … followed by an unchecked conversion

    Casting List<A> => List<B> is allowed by this rule

    List<A> => List   // narrowing reference conversion
    List => List<B>   // unchecked conversion
    

    That’s not all the story though; the spec has further rules to forbid casting like List<String>=>List<Integer>, for they are provably distinct parameterized
    types
    . There is no object belonging to the two types at the same time, so compiler think it’s better to disallow this apparent programming error. (You can bypass it by explicitly List<String>=>List=>List<Integer>)

    The last rule doesn’t apply here though; so it looks like a javac7 bug.

    Why the last rule doesn’t apply: so we are casting List<? extends A> to List<A>. Here capture conversion is applied to List<? extends A> [2] so we are actually casting List<T> to List<A>, where T is a new type variable with upper bound A.

    The question is whether List<T> and List<A> are provably distinct parameterized types. My understanding is that it’s false (it has to be false for your first two examples to compile). Since T is a type variable, it can take a value to make List<T> and List<A> the same parameterized type (i.e. when T=A). This reasoning should work for any type A.

    [1] http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5

    [2] http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#341306

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

Sidebar

Related Questions

in c++ i have following code class Foobar{ public: Foobar * operator()(){ return new
I have the following code class Program { static void Main(string[] args) { List<A>
I have the following code class OverlayTask extends AsyncTask<Void, Void, Void> { @Override public
Imagine the following code: class foreach_convert { public static void method2() { List<IComparable> x
I have the following code class sample { public void update(List l) { l
Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
Consider the following code: class Program { static void Main(string[] args) { Department deathStar
I have following code class User attr_accessor :name end u = User.new u.name =
Consider the following code: class A { public: virtual void f() throw ( int

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.