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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:24:38+00:00 2026-05-26T01:24:38+00:00

I have the following question regarding the code below: public class GenericBridgeMethods <T> {

  • 0

I have the following question regarding the code below:

public class GenericBridgeMethods <T> {

    public static void main(String[] args) {
        List obj = new ArrayList<Integer>();
        List <?> l1 = (List<?>) obj; // clause 1
        GenericBridgeMethods <?> g1 = (GenericBridgeMethods<?>) obj; // clause 2
   }

}

a. Clause 1 of course won’t give an unchecked cast warning
b. Clause 2 also did not give an unchecked cast warning

I noticed that a cast from a raw type (obj) to a ANY reifiable type (like GenericBridgeMethods or GenericBridgeMethods <?>) will not give a unchecked cast warning. If you run this code, a runtime error will occur at clause 2.

Shouldn’t the compiler give a warning at clause 2

EDIT 1:

    ArrayList a1 = new ArrayList<Integer>(); // clause 3
    Number n1 = (Number)a1; // clause 4 ERROR
    Comparable c1 = (Comparable)a1; // clause 5

    List l1 = new ArrayList<Integer>(); // clause 6
    Number n2 = (Number)l1; // clause 7
    Comparable c2 = (Comparable)l1; // clause 8

Can anyone explain why only clause 4 has 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-26T01:24:38+00:00Added an answer on May 26, 2026 at 1:24 am

    Well, first off in GenericBridgeMethods as you have defined it, T is not a reifiable type. Reifiable means that the type will be encoded into the class and will be available at runtime. That is not true of T.

    Clause 2 does not give a runtime warning because it is checked: There will be a runtime check that obj is type-assignable to the GenericBridgeMethods type. Since you’ve opted for a wildcard as the type parameter, nothing about T needs to be checked.

    If on the other hand you did something like this:

    GenericBridgeMethods<String> g1 = (GenericBridgeMethods<String>) obj;
    

    that would give you an unchecked assignment warning because the fact that obj is a GenericBridgeMethods of Strings cannot be checked at runtime. However, the same warning would appear if you had done this:

    List<String l1 = (List<String>) obj;
    

    Edit

    If you’re confused as to why the compiler allows you to try to cast a List to a GenericBridgeMethods, the answer is because the compiler can’t know the entire hierarchy of GenericBridgeMethods and its subclasses. There could be a subclass of GenericBridgeMethods that implements List, in which case the cast might be legitimate.

    You will however get a compile error if you made GenericBridgeMethods a final class (and thus prevented it from having subclasses). In this case, you will get an inconvertable types error.

    Just to show you your question has little to do with reifiable types and generics, take a look at this:

    public static void main(String[] args) {
       List obj = new ArrayList<Integer>();
    
       //this is allowed (no warning), even though it will fail at runtime
       CharSequence sequence = (CharSequence) obj; 
    }
    

    You can explicitly cast obj to a CharSequence even though you know that it will fail at runtime. The reason is because all the compiler knows is that obj is a type of List. Since List is an interface, there could be an implementation of CharSequence that is also a List, and so the cast must be permitted.

    Every explicit cast carries a degree of possibility that it could fail at runtime. Otherwise, it would be a redundant cast and the compiler should allow you to omit the explicit cast.

    Edit – Regarding your “edit #1”

    ArrayList a1 = new ArrayList<Integer>(); // clause 3
    Number n1 = (Number)a1; // clause 4 ERROR
    Comparable c1 = (Comparable)a1; // clause 5
    
    List l1 = new ArrayList<Integer>(); // clause 6
    Number n2 = (Number)l1; // clause 7
    Comparable c2 = (Comparable)l1; // clause 8
    

    You are wondering why only “clause 4” does not compile. I think I explained this already above and in the comments, but I’ll go thsough this specific example for you step-by-step.

    ArrayList a1 = new ArrayList<Integer>(); // clause 3
    Number n1 = (Number)a1; // clause 4 ERROR
    

    Casting a1 to Number does not work because Number and ArrayList are both classes, not interfaces. Because Java does not allow inheritance from multiple classes, for an object to be an instance of both Number and ArrayList, Number would have to be a subclass of ArrayList or vice versa. This is known to not be true at compile time.

    ArrayList a1 = new ArrayList<Integer>(); // clause 3
    Comparable c1 = (Comparable)a1; // clause 5
    

    Since Comparable is an interface, a subclass of ArrayList might be a Comparable.

    List l1 = new ArrayList<Integer>(); // clause 6
    Number n2 = (Number)l1; // clause 7
    

    Since List is an interface a subclass of Number could implement List. The compiler does not know when checking the cast that l1 holds an ArrayList.

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

Sidebar

Related Questions

I have a question regarding the following code: abstract class a { public static
I have the following code regarding asynchronous sockets programming: public void ServerBeginAceept(ushort ServerPort) {
I have a question regarding the following code snippet I came across in one
another noob question regarding F#. If I have the following code... let ExeC =
I have a question regarding the following code: while((c = getc(pFile)) != EOF) {
Quick question regarding EventHandlers in C#, let's say we have the following code: MyObject.MyEventHandler
I have a question regarding exception handling. Consider following Java code snippet. try{ //code
I have a question regarding memory allocation order. In the following code I allocate
I'm writing a small webapp in Grails and I have the following question regarding
Following on from my recent question regarding parsing XML files in Java I have

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.