Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,149 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,702 people online right now. Registration is fast and FREE... Join Now!




instanceof keyword

 
Reply to this topicStart new topic

> instanceof keyword, How to use the instanceof keyword in Java

BigAnt
Group Icon



post 2 Mar, 2009 - 09:56 AM
Post #1



Using the instanceof keyword

In Java there is a useful keyword called instanceof.

Why is this keyword useful? It is useful because it determines whether or not a given Object is an
instance of a given class.

The syntax for the instanceof keyword is as follows:
object_name instanceof class_name
where:
-->object_name is a reference to some Object
-->class name is a name of some Java class

So in the following code:
java

String myString = "I am a String";
if(myString instanceof String) {
System.out.println("I am an instance");
} else {
System.out.println("I am not an instance");
}


Will print out I am an Instance because myString is an instance the String class.

Now you have to be careful, consider the following code:
java

String myString = null;
if(myString instanceof String) {
System.out.println("I am an instance");
} else {
System.out.println("I am not an instance");
}


So you will say, myString is of type String it should print out I am an Instance. Then you run the code
and it prints out I am not an instance What gives, you say scratching you head.

This is because the created reference, myString, is a null reference, and thus points to no object. So
using the instanceof keyword on any null reference will result in a boolean value of false.

Inheritance and instanceof

Now you know how to use the instanceof keyword, but another thing to note is how the instanceof
keyword works in an inheritance hierarchy.

Well, since in Java if you have the superclass Animal, and the subclass dog you can create a dog like this: Animal myDog = new Dog();
It makes sense that in Java the instanceof keyword will tell you that myDog is an instance of dog, but it will also tell you that myDog is an instance of
Animal.

So what does this tell you about the instanceof keyword? The instance of keyword will return true for any class that the Object is created from, and will
also return true for any superclass of that class. But the reverse of this is not true, if you create an instance of animal like: Animal myDog = new Animal();
The instanceof will return true for the Animal class, but false for the Dog class.

This is because in Java all subclasses have to be aware of their superclasses, because they inherit from them. But the superclasses do not have to
be aware of their subclasses, because the subclasses have no effect on the operation of the superclass.

This idea can also be expanded through multiple superclasses, just remember that instanceof will only travel up the inheritance hierarchy,starting at
the class the Object is created from, never down. So sibling classes of the given class will not return true for instance of. So continuing the
Animal example, if you now have two subclasses of animal Cat and Dog, and you do:
Animal myDog = new Dog();
Animal myCat = new Cat();

The instanceof will return true for the myDog Object for the Dog and Animal classes but false for the Cat class. Following this the instanceof will
return true for the myCat object for the Animal and Cat classes, but false for the Dog class.

One final note:
All classes in Java inherit from the Object super class. So as you might assume using instanceof with the Object class will return true for any
and all objects you create. Just remember that for null references the instanceof will return false.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

bbq
Group Icon



post 16 Apr, 2009 - 08:16 PM
Post #2
Pretty useful indeed, thanks for spending the time structuring a nice tutorial on the use of this smile.gif

learn something new everyday smile.gif

more here than at uni thats for sure tongue.gif
Go to the top of the page
+Quote Post

watson49
*



post 21 Apr, 2009 - 10:34 PM
Post #3
QUOTE(BigAnt @ 2 Mar, 2009 - 09:56 AM) *


Using the instanceof keyword

In Java there is a useful keyword called instanceof.

Why is this keyword useful? It is useful because it determines whether or not a given Object is an
instance of a given class.

The syntax for the instanceof keyword is as follows:
object_name instanceof class_name
where:
-->object_name is a reference to some Object
-->class name is a name of some Java class

So in the following code:
java

String myString = "I am a String";
if(myString instanceof String) {
System.out.println("I am an instance");
} else {
System.out.println("I am not an instance");
}


Will print out I am an Instance because myString is an instance the String class.

Now you have to be careful, consider the following code:
java

String myString = null;
if(myString instanceof String) {
System.out.println("I am an instance");
} else {
System.out.println("I am not an instance");
}


So you will say, myString is of type String it should print out I am an Instance. Then you run the code
and it prints out I am not an instance What gives, you say scratching you head.

This is because the created reference, myString, is a null reference, and thus points to no object. So
using the instanceof keyword on any null reference will result in a boolean value of false.

Inheritance and instanceof

Now you know how to use the instanceof keyword, but another thing to note is how the instanceof
keyword works in an inheritance hierarchy.

Well, since in Java if you have the superclass Animal, and the subclass dog you can create a dog like this: Animal myDog = new Dog();
It makes sense that in Java the instanceof keyword will tell you that myDog is an instance of dog, but it will also tell you that myDog is an instance of
Animal.

So what does this tell you about the instanceof keyword? The instance of keyword will return true for any class that the Object is created from, and will
also return true for any superclass of that class. But the reverse of this is not true, if you create an instance of animal like: Animal myDog = new Animal();
The instanceof will return true for the Animal class, but false for the Dog class.

This is because in Java all subclasses have to be aware of their superclasses, because they inherit from them. But the superclasses do not have to
be aware of their subclasses, because the subclasses have no effect on the operation of the superclass.

This idea can also be expanded through multiple superclasses, just remember that instanceof will only travel up the inheritance hierarchy,starting at
the class the Object is created from, never down. So sibling classes of the given class will not return true for instance of. So continuing the
Animal example, if you now have two subclasses of animal Cat and Dog, and you do:
Animal myDog = new Dog();
Animal myCat = new Cat();

The instanceof will return true for the myDog Object for the Dog and Animal classes but false for the Cat class. Following this the instanceof will
return true for the myCat object for the Animal and Cat classes, but false for the Dog class.

One final note:
All classes in Java inherit from the Object super class. So as you might assume using instanceof with the Object class will return true for any
and all objects you create. Just remember that for null references the instanceof will return false.

Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 04:17PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month