• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning references problem

 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sunny X Narula
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)...
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit
That settles it for me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic