• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

ClassCastException on List<Integer>

 
Greenhorn
Posts: 6
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can someone explain why the second code block throws a ClassCastException?






Regards,
Jed
 
Marshal
Posts: 26128
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The toArray() method returns an Object[], not an Integer[]. This is why the ClassCastException is thrown, and the error message says exactly that for me. And that happens with both of your examples.
 
Jed Colina
Greenhorn
Posts: 6
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:The toArray() method returns an Object[], not an Integer[]. This is why the ClassCastException is thrown, and the error message says exactly that for me. And that happens with both of your examples.



Can you try and run it again? the first example actually works. But the ONLY second one throws a ClassCastException.
 
Paul Clapham
Marshal
Posts: 26128
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jed Colina wrote:Can you try and run it again? the first example actually works. But the ONLY second one throws a ClassCastException.



Sure.

(Runs code.)

java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Integer; ([Ljava.lang.Object; and [Ljava.lang.Integer; are in module java.base of loader 'bootstrap')
at test.Test.main(Test.java:14)



But let's see your complete and compilable code which demonstrates the absence of that exception. I just pasted your code fragment into an existing test program.
 
Paul Clapham
Marshal
Posts: 26128
77
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually that's a very interesting question. I changed my IDE to compile your code using Java 8 and it works like you said -- the type of intList is Integer[] and the type of myList is Object[].

Then I changed it to compile using Java 9 and it works like I said -- the types of intList and myList are both Object[]. I'm using Java 15 so that explains why I got the output I did. And you must be using Java 8 (or earlier).

The question is very interesting because it suggests that there's a bug in the Java runtime, and the managers of Java agreed with you because they wrote up a bug report for it: https://bugs.openjdk.java.net/browse/JDK-6260652 and fixed that bug in Java 9.

Not every Java programmer gets to sniff out a Java bug, so have a cow for your post!
 
Jed Colina
Greenhorn
Posts: 6
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Jed Colina wrote:Can you try and run it again? the first example actually works. But the ONLY second one throws a ClassCastException.



Sure.

(Runs code.)

java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Integer; ([Ljava.lang.Object; and [Ljava.lang.Integer; are in module java.base of loader 'bootstrap')
at test.Test.main(Test.java:14)



But let's see your complete and compilable code which demonstrates the absence of that exception. I just pasted your code fragment into an existing test program.





 
Paul Clapham
Marshal
Posts: 26128
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like I was posting at the same time as you. Have a look at my latest reply.
 
Saloon Keeper
Posts: 6709
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:The question is very interesting because it suggests that there's a bug in the Java runtime, and the managers of Java agreed with you because they wrote up a bug report for it: https://bugs.openjdk.java.net/browse/JDK-6260652 and fixed that bug in Java 9.


... and it took them 10 years to fix :-(
 
Sheriff
Posts: 22041
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At least they were faster with the bug I found earlier this year: java.util.Properties.entrySet() does not override java.lang.Object methods. Although it's been in the JDK from Java 9 until 15 (fixed in 16), it was fixed within 2 months after it was reported.
 
BWA HA HA HA HA HA HA! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic