aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassCastException" Watch "ClassCastException" New topic
Author

ClassCastException

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, Why I am getting this exception.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39


It compiles for me.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, This question is about Runtime ClassCastException. It throws ClassCastException because TreeSet cannot sort its elements (Double and Integer) at Runtime. My question is, How do we know that 0xCAFE is an Integer.
Henry Wong wrote:
It compiles for me.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

Saibabaa Pragada wrote:Hi, This question is about Runtime ClassCastException. It throws ClassCastException because TreeSet cannot sort its elements (Double and Integer) at Runtime. My question is, How do we know that 0xCAFE is an Integer.


Oops.... apologies. Wasn't paying attention.

Literals that start with 0x are hexidecimal numbers. They are integer literals, unless they end with L, then they are long literals.

Henry
Imad Aydarooos
Ranch Hand

Joined: Nov 02, 2010
Posts: 87
Dear Saibabaa, actually the problem is caused by the line (add(3.7)) which is a double autoboxed into Double , when it compared to the Integer value provided by the second line the stack trace is:
at java.lang.Integer.compareTo(Integer.java:35)
which states clearly that we cannot compare a Double to Integer
NOTE: any integral literal is of type int, any floating point literal is of type double

Regrads


love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Henry Wong wrote:
Saibabaa Pragada wrote:Hi, This question is about Runtime ClassCastException. It throws ClassCastException because TreeSet cannot sort its elements (Double and Integer) at Runtime. My question is, How do we know that 0xCAFE is an Integer.


Oops.... apologies. Wasn't paying attention.

Literals that start with 0x are hexidecimal numbers. They are integer literals, unless they end with L, then they are long literals.

Henry


Henry is correct. Because, you've stored a double value into the collections, which takes abstract Number as the parameter, we unable to compare the first Double and the Integer. Have a look on the below code snaps!



|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException
 
Similar Threads
for each loop
TreeSet contains method doesn't work for me??????
Binary tree
Print Object output incorrect - Head First Java Chapter 16 TreeSet Exercise
testtree.Book cannot be cast to java.lang.Comparable