File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class CAst  Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class CAst  Exception" Watch "Class CAst  Exception" New topic

Class CAst Exception

ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
It will be great help if some one please explain why the below code throwing Class Cast Exception:

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));
class Person{
Person(int i){}

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
A TreeSet requires that either the elements you insert into have to be mutually comparable, i.e. the class definition implements the Comparable interface, or that you create the TreeSet with a Comparator that specifies how two objects in the set will be compared.
ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
Thanks Keith ..i got it.
I agree. Here's the link:
subject: Class CAst Exception
It's not a secret anymore!