• 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

Jist of Rules

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can anyone give me a jist of important rules to be kept in mind while casting object references and using the instanceof operator.

I don't seem to understand the logic behind the instanceof operator.

Also please clarify...
Is the instanceof operator used to check what object a reference will denote at runtime ?

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply put, is the reference an instance of a certain type, Can that reference be assigned to another reference of that type.

It is all about inheritence.

So, if you do instanceof Object, to any oreference variable, then that will always be true.

So let's make up a Hierarchy.

Object
|
Vehicle
/ \
Car Truck
| |
Jetta S10

So the following return true

Car a = new Car();
a is an instance of Car, Vehicle, and Object. But not an instance of Jetta.
Car a = new Jetta();
a is an instance of Car, Vehicle, and Object. And is an instance of Jetta.

So the actual type, not the reference type actually determines the instance of. So anything that is of that type or of a super type of that type will pass instanceof.

Hope that helps.

Mark
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean b = (ref1 instanceof ClassType);

The above expression is simply read as follows:

if ref1 is actually an INSTANCE of ClassType, then return true, if not then you guessed it... false.

But remeber than ref1's type and ClassType must be found in the same class heirarchy if not there will be a compile-time error.

If ClassType were an InterfaceType, then there will be no problem.
 
Marcelo Ortega
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well put Mark.
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
Simply put, is the reference an instance of a certain type, Can that reference be assigned to another reference of that type.

It is all about inheritence.

So, if you do instanceof Object, to any oreference variable, then that will always be true.

So let's make up a Hierarchy.

Object
|
Vehicle
/ \
Car Truck
| |
Jetta S10

So the following return true

Car a = new Car();
a is an instance of Car, Vehicle, and Object. But not an instance of Jetta.
Car a = new Jetta();
a is an instance of Car, Vehicle, and Object. And is an instance of Jetta.

So the actual type, not the reference type actually determines the instance of. So anything that is of that type or of a super type of that type will pass instanceof.

Hope that helps.

Mark




Okay Mark...I seem to have understood something.

Now, if I say,
Car a = new Car();
Jetta b = new Jetta();

and then I say
b = a;

What am I doing ? What would (b instanceof a) give and why ?
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now, if I say,
Car a = new Car();
Jetta b = new Jetta();

and then I say
b = a;



well you can't assign a to b, because a is a Car, and b is a Jetta. You do not know that a is a Jetta because the instance is of Car and the reference is of Car

if you had

Car a = new Jetta();

then you can assign a to b, but it will require for an explicit cast in the line as in

Jetta b = (Jetta)a;

And as far as (b instanceof a) You can't do that because you have to have a Class not a reference on the right side of instanceof.

but b is an instanceof Car.

Mark
 
Oh the stink of it! Smell my 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