This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Assigning references problem

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9690
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit
That settles it for me.
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic