This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jist of Rules  RSS feed

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well put Mark.
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
Evacuate the building! Here, take this tiny ad with you:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!