Page 1 of 1

instanceof keyword How to use the instanceof keyword in Java Rate Topic: -----

#1 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Posted 02 March 2009 - 10: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:
		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:
		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.

Is This A Good Question/Topic? 0
  • +

Replies To: instanceof keyword

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Posted 16 April 2009 - 09:16 PM

Pretty useful indeed, thanks for spending the time structuring a nice tutorial on the use of this :)

learn something new everyday :)

more here than at uni thats for sure :P
Was This Post Helpful? 0
  • +
  • -

#3 watson49  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-April 09

Posted 21 April 2009 - 11:34 PM

View PostBigAnt, on 2 Mar, 2009 - 09:56 AM, said:


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:
		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:
		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.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1