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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:51:59+00:00 2026-06-11T02:51:59+00:00

Language: Java Compiler version: 1.6 In the below code, am trying to do the

  • 0

Language: Java
Compiler version: 1.6

In the below code, am trying to do the following:

  1. create a List<String>
  2. add a String
  3. assign List<String> to raw List
  4. create a List<Integer>
  5. assign the raw List to List<Integer>
  6. add an Integer
  7. retrieve the value using get() @ indexes 1 & 2 and print them.

All statements are compiling (with warnings) and run fine.

But if I try to loop through the List<Integer> using a for loop, I am getting a ClassCastException. I am just wondering why its allowed me to use list.get() method but not allowing me to iterate over it?

Output: (if I run with un-commented for loop) abcd 200

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot  be cast to java.lang.Integer
        at genericsamples.CheckRawTypeAdd.main(CheckRawTypeAdd.java:26)

Here is my code

import java.util.*;
import java.io.*;
class CheckRawTypeAdd
{
    public static void main(String[] sr)
    {   
        List<String> list_str = new ArrayList<String>();
        list_str.add("abcd");
        List<Integer> list_int = new ArrayList<Integer>();  
        List list_raw; 
        list_raw=list_str;
        list_int=list_raw;
        list_int.add(200);
        Object o1 = list_int.get(0);
        Object o2 = list_int.get(1);        
        System.out.println(o1);
        System.out.println(o2);
        //for(Object o : list_int)
        //{
        //  System.out.println("o value is"+o);
        //}
    }
}
  • 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-06-11T02:52:01+00:00Added an answer on June 11, 2026 at 2:52 am

    I would consider this a compiler bug in javac. A checked cast is getting inserted. We can see this using javap -c CheckRawTypeAdd to disassemble the class (cast is 101; note that I took out some of the unneeded lines of code before compiling, so code points will vary):

      77: invokeinterface #10,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      82: astore        6
      84: aload         6
      86: invokeinterface #11,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      91: ifeq          109
      94: aload         6
      96: invokeinterface #12,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     101: checkcast     #13                 // class java/lang/Integer
    

    However, the Java Language Spec (14.14.2) indicates that this cast should be to Object, not Integer. It starts by defining the terms via the grammar:

    EnhancedForStatement:
        for ( FormalParameter : Expression ) Statement
    
    FormalParameter:
        VariableModifiersopt Type VariableDeclaratorId
    
    VariableDeclaratorId:
        Identifier
        VariableDeclaratorId []
    

    So in our case, Type is Object. Then it goes on to say what this gets translated into:

    for (I #i = Expression.iterator(); #i.hasNext(); ) {
        VariableModifiersopt TargetType Identifier =
            (TargetType) #i.next();
        Statement
    }
    

    So what’s relevant here is the resolution of TargetType. This is also defined in the JLS:

    If Type (in the FormalParameter production) is a reference type, then TargetType is Type

    As Object is most certainly a reference type, then TargetType is Object and so the checked cast should be to Object, not Integer.

    That this is a bug is further evidenced by others in this thread noting that this problem doesn’t occur if ecj (Eclipse’s compiler) is used. However, I understand that this would be a low priority bug for the Oracle compiler team since you have to abuse generics to exercise it. One would almost say it’s a feature, not a bug.

    Follow-up

    To give final confirmation that it’s a bug, here’s an existing bug report for this exact issue:

    • Bug ID: 6500701 – Enhanced for loop with generics generates faulty bytecode

    Also, I should note two things. First, the JLS references I gave above were in the latest JLS, and that section has actually changed for Java 7 (in response to this bug!)

    Here’s what an enhanced for statement should have translated to for Java 6 and earlier:

    for (I #i = Expression.iterator(); #i.hasNext(); ) {
            VariableModifiersopt Type Identifier = #i.next();
       Statement
    }
    

    As you can see, there is no checked cast specified here. So the bug in javac wasn’t that it was doing the wrong cast, it was that it was doing any cast at all.

    Second, in Java 7, javac correctly compiles the code according to the JLS SE 7 spec (which is what I cited above). Thus, the following code works:

    List<String> list_str = new ArrayList<String>();
    ((List) list_str).add(new StringBuilder(" stringbuilder"));
    for (CharSequence o : list_str) {
       System.out.println("o value is" + o);
    }
    

    With a correct cast to CharSequence, not String. I was using JDK 6 to compile initially, not JDK 7.

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

Sidebar

Related Questions

A Java method call may be parameterized like in the following code: class Test
What search order does the Java compiler use to resolve Foo in the following
Programming language: Java Ok, so I want to have a BufferedImage that keeps rotating
i am a beginner at java language and i use text pad. i have
i am still new to the Java language and libraries... i often use this
Based on my understanding of the Java language, static variables can be initialized in
I was searching a bit for another platform independent language like Java. Are there
As you maybe know, as Java language perspective all method in C# are final
I have a Scala/Java dual language project where I need to pass a Scala
I am implementing parser for language similar to java but simpler. I have written

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.