This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes When does Class Cast Exception occur? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When does Class Cast Exception occur?" Watch "When does Class Cast Exception occur?" New topic
Author

When does Class Cast Exception occur?

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192

When I compile and run the code presented above then a
Class Cast Exception at f = (Float)ob occurs.
C:\Java\EigeneJavaProgramme>java forex2
Float is Subclass of Object
java.lang.ClassCastException: java.lang.Object
at forex2.main(forex2.java:9)
Exception in thread "main"
I don’t understand that. I always thought a class cast exception
is only thrown if you explicitly cast an object into an instance of
another class which is not related by inheritance
Appreciate your answers.
Thomas
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192

C:\Java\EigeneJavaProgramme>java CastTest2a
java.lang.ClassCastException: Tree
at CastTest2a.main(CastTest2a.java:10)
Exception in thread "main"
Trish Wu
Ranch Hand

Joined: Oct 09, 2002
Posts: 34
//yes, a Float wrapper class is an object, so you can assign an Object reference to this Float object.
Object objRef = new Float(9f);
//The object never forget who she is (Float), so this will call the toString() method in the Float object
System.out.println(objRef.toString());
//now I think I need to invoke method only available in the Float Object, So i assign it to a Float reference
//As i am doing down casting, I need to make an explicit cast like this:
Float ft = (Float) objRef;
The code u have in your first posting will throw a class cast exception because the first object u are creating is an Object (it will always be). It is not a Float wrapper, so it wont work if u down cast it to a Float.
But u can change a string to a float with the following code:
String s = "9F";
Float f = Float.valueOf(s);
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Why doesn't this throw ClassCastException?

Compiles fine and runs fine in comparison to ClasTest2a which throws ClassCastException.
Why?
Thomas
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Because t is an AppleTree object. It is simply assigned to a pointer of type Tree.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
It is simply assigned to a pointer of type Tree.
Ba! Mr. C# probably meant to say that it is assigned to a reference of type Tree.
When does Class Cast Exception occur?
From the documentation (mostly): A ClassCastException is thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:
Object x = new Integer(0);
System.out.println((String)x);
Where as the following code would not create a ClassCastException:
Object x = new Integer(0);
System.out.println((Integer)x);


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello,
may I understand it this way:
If I EXPLICITLY cast an instance of a class into an instance of another class then a ClassCastException will occur if it is no SUBCLASS.
Is this ok?
Thomas
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Maybe, kinda, almost.
It really helped me (and what I stress whenever I'm teaching Java concepts to new programmers is) to learn to recognize the seperate concept of the object and the reference to the object.
You cannot change the type of an object. A casting operator doesn't change the type of an object. It takes a reference of some type, and creates a reference of another type that refers to the same object that the original reference referenced. If the object is not actually of the type that you made a reference for, then a ClassCasteException will occur.
Did that help any?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When does Class Cast Exception occur?
 
Similar Threads
Roundup q.26 assignmtn of superclass to subclass
obj conversion
confusing Generics
Casting and Conversions
Floating-point Operations