| Author |
what is ClassCastException...any body plz Explain?
|
veesam sridhar
Ranch Hand
Joined: Oct 18, 2005
Posts: 51
|
|
Hi, Can any body give some Explanation for the following Question? //************************** import java.util.*; public class Test1{ public static void main(String a[]){ Set s = new TreeSet(); s.add(new Person(20)); s.add(new Person(10)); System.out.println(s); } } class Person{ Person(int i){} } //********************* What is the output? The answere is ClassCastException. Can any body tell me what is that and when it comes. Thanks in advance
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
If you check the API for TreeSet, you will see...
This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.
This means that any type of object you put in a TreeSet must be capable of being ordered -- that is, "compared" to another object of its type. This can be done in two ways.... The TreeSet can be provided with a Comparator at the time of instantiation (which does not happen here, because the no-args constructor is used), OR...The objects themselves can implement Comparator (which they do not here).So when the second object is added on line 6 of your code (which is where the compiler should tell you the problem is), an attempt is made to compare these objects for ordering. The CastCastException occurs because a Person is not Comparable.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Take a look at this code. I've modified the Person class to implement Comparable (with a compareTo method). I've also overridden toString so that you can see meaningful results when the set is printed. You will see that the set order is maintained based on the Person's int value. [ October 27, 2005: Message edited by: marc weber ]
|
 |
veesam sridhar
Ranch Hand
Joined: Oct 18, 2005
Posts: 51
|
|
|
Thanks marc weber
|
 |
Seb Mathe
Ranch Hand
Joined: Sep 28, 2005
Posts: 225
|
|
Originally posted by marc weber: This means that any type of object you put in a TreeSet must be capable of being ordered -- that is, "compared" to another object of its type. This can be done in two ways.... The TreeSet can be provided with a Comparator at the time of instantiation (which does not happen here, because the no-args constructor is used), OR...The objects themselves can implement Comparator (which they do not here).
When there's no Comparator provided to the TreeSet constructor, elements must implements Comparable, not Comparator (as you can see in mark's example )  [ November 03, 2005: Message edited by: Seb Mathe ]
|
Regards,<br />Seb<br /> <br />SCJP 1.4
|
 |
veesam sridhar
Ranch Hand
Joined: Oct 18, 2005
Posts: 51
|
|
I got the point, Thankyou guys.
|
 |
 |
|
|
subject: what is ClassCastException...any body plz Explain?
|
|
|