Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime vs Compile time

 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
Dan
[ October 18, 2005: Message edited by: Dan Bromberg ]
 
jaspal singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

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.
 
Martin Wingert
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Dan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic