File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Runtime vs Compile time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime vs Compile time" Watch "Runtime vs Compile time" New topic

Runtime vs Compile time

Dan Bromberg
Ranch Hand

Joined: Oct 18, 2005
Posts: 51
I need to get a better grasp of the difference between 'runtime' &
'compile time', other than the obvious connotation. In particular, I'm confused how the JVM will compile a program with no errors but at runtime the program could get a ClassCastException error.

For example, below is some code from Schaums JAVA DATA STRUCTURES where I don't understand the author's comment, 'RUN-TIME ERROR: xx cannot be cast as a Y'; since the "yy = (Y)xx;" cast is OK why does the program croak on the last line?

class X { public String toString() { return "I am an X."; } }
class Y extends X { public String toString() { return "I am a Y."; } }

public class Ex0304
{ public static void main(String[] args)
{ Y y = new Y();
System.out.println("y: " + y);
X x = y;
System.out.println("x: " + x);
Y yy = (Y)x; // casts x as type Y
System.out.println("yy: " + yy);
X xx = new X();
System.out.println("xx: " + xx);
yy = (Y)xx; // RUN-TIME ERROR: xx cannot be cast as a Y
System.out.println("yy: " + yy);

Thanks much!
[ October 18, 2005: Message edited by: Dan Bromberg ]

Thanks in advance to all who reply,
jaspal singh
Ranch Hand

Joined: Sep 15, 2005
Posts: 54

the answer is very obvious if u look the inheritance hierarchy.

since x is super to y and y has its additional features than x, if u cast an
instance of y to x, x cant know the details of it.

Y y1 = new Y();
X x1;

x1=y1;-->will throw runtime exceptoion since y1 is orginally Y's instance.

but consider following;

X x1=new X();
Y y1 = x1;

x1=y1;-->will work correctly since y1 actually points to super class instance.

Teach me the art of forgetting, for I always remember wat I have forgotten.
Martin Wingert

Joined: Oct 10, 2005
Posts: 16
This has to do with upcasting and downcasting of objects.

Upcasting is if you create an object Y and then cast it as an X.
Thats ok since Y extends X its ok to point to the Y object with an X pointer (lots of X and Y here :-)

Downcasting is the other way around i.e you cast an X as an Y. This is only ok if the X pointer already points to an Y object. There is an example of this in your code:

Y yy = (Y)x; // casts x as type Y

This is ok since the x pointer infact points to the Y object created on the first line of the main method.

But then you create an X object and tries to cast it to a Y reference. This will generate an ClassCastException when you run the program and it tries to make the cast.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The key point is that the compiler doesn't -- and in the most general case, can't -- determine the exact type of the object in xx in the line

yy = (Y) xx;

In this program, a smart compiler could figure it out, because a few lines before a new X object is assigned to xx. But imagine if xx was passed in to this method as a parameter, in which case the compiler would have no way to know. Becuase it's not always possible, the compiler just doesn['t try to take more than the one line above into account. It's possible that this will succeed, so it compiles. Then it fails at runtime when the true type becomes blindingly obvious.

[Jess in Action][AskingGoodQuestions]
Dan Bromberg
Ranch Hand

Joined: Oct 18, 2005
Posts: 51
Thanks to all replies. I somehow missed the point that 'downcasting requires the super object to already point to a child object'. I thought a cast could be done without this requirement but apparently not. I saw the author had done this earlier in his example but needed a confirmation that this always needs to be done.

Thanks again for your time, guys.
I agree. Here's the link:
subject: Runtime vs Compile time
It's not a secret anymore!