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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Set question " Watch "Set question " New topic
Author

Set question

Arad Chear
Ranch Hand

Joined: Jan 05, 2007
Posts: 98


since Double is-a Number
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Yes, Double is a Number. But the elements of a TreeSet have to be mutually comparable.
Arad Chear
Ranch Hand

Joined: Jan 05, 2007
Posts: 98
thanks so much lynn ,i got it
m ali
Ranch Hand

Joined: Apr 12, 2007
Posts: 49
Hi Keith,

But we are declaring here the collection as type-safe number. Integer is a Number and Double is a Number? Why the above code fail to compile? please explain?

Thanks in advance.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


No, Ali, Did you compile the above code?
This code compiles fine but gives runtime exception because
of mutual incomparability between treeset objects. One object
is Integer and another is Double.

TreeSet uses Comparable interface's compareTo(...) method that
is implemented by the objects inserted into, tokeep the objects
in sorted manner. And an Integer and an Double can't be compared
because they are not mutually comparable as Keith said
already.




Regards,
cmbhatt
[ April 24, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
m ali
Ranch Hand

Joined: Apr 12, 2007
Posts: 49
Thank you Chandra,

I got it.
Zhao zhenhua
Ranch Hand

Joined: Jul 17, 2006
Posts: 37

Thanks ,Chandra Bhatt.Yes!You are right!


Head first Servlet and JSP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set question
 
Similar Threads
Whizlab mock: ClassCasting
is LinkedHashSet ordered?
Generics
Mock question of collection's toArray method
What is the Use of NavigableMap and NavigableSet?