wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assigning references problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assigning references problem" Watch "Assigning references problem" New topic
Author

Assigning references problem

Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72


This programme is correct and compiles and runs fine
but i want to know how things are being assigned to each other(in line 1,2,3,4,5,6,7)

Can somebody please explain how its all is happening,as this is something very confusing to me
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You are shifting types back and forth. Since m is initialized with null, the program will execute fine and all variables will hold null.
The casting always changes the type of object that is assigned to each reference variable.


JDBCSupport - An easy to use, light-weight JDBC framework -
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Before casting what is the thing we basically do...... isn't it the instanceof. In instanceof on the left side there can be any non final class and on the right side there can be any interface or the class in the same inheritance tree. So apart from inconvertible types rest everything will compile.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Sunny X Narula
Greenhorn

Joined: Dec 07, 2008
Posts: 22
Could anyone post an example of inconvertible types which is not a primitive type.
Or is it so that there are none.
Which would explain why this compiles but does not run:
Yewint Ko
Ranch Hand

Joined: Aug 02, 2009
Posts: 90
hi SunnyNar !
let me try.
You should check "instanceof" test whenever you cast.
At line 2 ,you cast m to C. so check "m instanceof C".
You need to think first that "the object of m is C?".
No, the object of m is not C ,it is just Anonimous that implements Min. ( Min m =new Min{} )You see?
This line ( b = (C) m ) will cause runtime exception.
Sunny X Narula
Greenhorn

Joined: Dec 07, 2008
Posts: 22
Let me try this again, when explicitly casting will any code that uses non primitive types ever fail at compile time.
Sorry if the my question was unclear.
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
Can somebody give one example to explain it in a more clear way, as what i understand from it doesn't work in other example,

Like what i got is

In line2
m instanceof c
In 3
b instance of Min
In 4
m instanceof A
in 5
a2 instanceof B
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

I've not read the thread completely but here's what you might be confused at

Suppose if there is an interface MyInterface and a class MyClass (they don't have any relation). Now you can type cast between them like this



This works because there might be a class MySubClass, which extends MyClass and implements MyInterface. This is why the cast is allowed. I might create a class like this



Then this code will run fine



This is why you are allowed to do a type cast and instanceof comparison between an interface and a class. But this will not work if suppose the class MyClass is final. This is because in that case MyInterface and MyClass cannot have a common sub-class (as MyClass cannot be sub-classed)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Keep it simple. Compiler checks the reference variable type. So the casting and the casted variable should be in the same inheritance tree. Also if the interface comes then the interface cannot have an object but its subclass can be which may extend someother class.
Sunny X Narula
Greenhorn

Joined: Dec 07, 2008
Posts: 22
Thanks Ankit
That settles it for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning references problem