• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help with basic cast concept ??

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.!
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic