wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help with basic cast concept ?? 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help with basic cast concept ??" Watch "help with basic cast concept ??" New topic
Author

help with basic cast concept ??

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
A a = new A();
Y y = new Y();
boolean b = a==y;
where A and Y are unrelated classes.
this should be compiling fine, but does not. i thought that the type is checked at runtime ? or even a=y;//does not compile. why ??
because if you take this example
interface II {} and class A. (unrelated again ie class A does not implement interface II)
A a = new A();II ii;
ii = (II)a;//this compiles fine, though would give runtime exception
so why does the first example does not even compile. why ? what is the difference between these two situations. I am missing some concept here.... please reply asap
[ February 10, 2002: Message edited by: mark stone ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
In the first example, the compiler is able to check the delcared types of your variables, and it knows that they are not compatible for assignment or comparison. It's no different than trying to do:
boolean b = false;
double d = 5.5;
if (d == b) //compiler error, incompatible types.
Remember that Java is a strongly typed language. That means you can't just assign everything to everything else. Assignment only works on compatible types.
In your interface example, a reference variable of an interface type has to hold an actual object at run time. So the compiler has to allow for the possibility that the actual object in a at runtime might be a subclass of a that implements the interface. It is "possible", so the compiler will allow it. Like you said, at runtime, a real check is made to see if your assignment makes sense, and if not you get an exception.


Rob
SCJP 1.4
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
Adding to Rob's post,
It wud help if u go through RHE Tables on Compiler Rules for Conversion of Refernce Types
and Compile and Runtime Rules for Casting of Reference Types.

Also, taking some things out of JLS, to specifically apply to the cases u have stated.
1>
A a = new A();
Y y = new Y();
boolean b = a==y;
where A and Y are unrelated classes.
Here, a==y is Reference Equality check.
A Compiler Error(CE) wud occur if it is imposible to convert the type of either operand to the other by a casting conversion ...
Casting Conversion Rules do not let two unrelated classes to be converted to one another.
So, u have a Compiler Error.
1.1> a=y also doesnt work.
Because for assignment conversion, only widening ref. conversions are allowed which doesnt let unrelated classes be assigned to each other.And the check is made at compile time!
2> interface II {} and class A. (unrelated again ie class A does not implement interface II)
A a = new A();II ii;
ii = (II)a;
Here, Rules of casting conversion apply .. because u are uwing the cast operator. [ (II)a ]
Casting conversion Rules allow at Compile Time for a class 'A' to be a an interface 'II'
if A is not final ... because there is a possibility that the variable 'a'(of class Type A) might hold a subclass of 'A' which may implement II.
But this check is resolved at runtime... which is why u get an exception!
2.1> But the same thing will not work for two unrelated classes.
a=(A)y .. will not work..gives CE!
Because u are casting here, and u just cannot cast unrelated classes .
The check is done at compile time itself.!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with basic cast concept ??
 
Similar Threads
cast to interface
Runtime & compilation errors
inner class help...
Please explain these answers
Runtime vs Compile time