GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jist of Rules Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jist of Rules" Watch "Jist of Rules" New topic
Author

Jist of Rules

Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
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 ?



<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

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.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

Well put Mark.
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
 
GeeCON Prague 2014
 
subject: Jist of Rules