objectName instanceof typeName
There is a space and no dot between objectName and instanceof. There are no parentheses around the second operand; there is a space and no dot between instanceof and typeName. instanceof is a relational operator in Java. In Java, relational operators are <, >, <=, >=, and instanceof . == and != are not relational operators in Java; they are equality operators. The instanceof operator can be used in the if-condition, as well as in the other relational operators.
There are two kinds of types in Java: primitive types and reference types. Primitive types are not used with the instanceof operator. There are four kinds of reference types which are the class types, interface types, type variables, and array types. The instanceof operator works with these four kinds of reference types. Primitive types are the exceptions to be used with the instanceof operator. This article illustrates the use of the instanceof operator.
Article Content
- Primitive and Primitive Wrapper Classes
- Predefined Types
- Class Reference
- abstract and interface Types
- Type Variable
- Conclusion
Primitive and Primitive Wrapper Classes
int is a primitive type. Integer is the wrapper class of the int primitive type. The output of the following program is true:
Integer, beginning with an uppercase of I, is a class reference, while int is a primitive type. The statement that uses the instanceof operator here is the second statement of the main() method. There has to be a space between obj and instanceof and between instanceof and the type.
In the following program, where int is used instead of Integer, a compiler error is issued:
The first statement of the main() method, should have begun with Integer instead of int. The second operand of instanceof, should have been Integer, also.
Other primitive types and their corresponding wrapper classes are related in the same way.
Predefined Types
The String type is an example of a predefined type. The output of the following program is true:
Note the way the first and second statements of the main() method have been coded.
Class Reference
The following program has a class reference, AClass. This works with the instanceof operator. The output of the program is true:
Note the way the first and second statements of the main() method have been coded.
Array Types
The array object is indirectly instantiated from the array type. The following program outputs true:
Note the way the first and second statements of the main() method have been coded. The array type is indicated with square brackets, next to the (primitive) type.
Abstract and Interface Types
An abstract method is typically a method signature, beginning with the reserved word, abstract, and ending with a semicolon.
Difference Between Abstract and Interface Classes
An abstract class is preceded by the reserved words, abstract and class. In interface is not preceded by these two words; it is preceded by just interface. An abstract method in an abstract class must be preceded by the reserved word, abstract. An abstract method in an interface class should not be preceded by the reserved word, abstract. A class extends an abstract class, while a class implements an interface.
Sub-Class and Abstract Type
In the following program, Sub is a sub-class (inherited) of the abstract class, Abst. The output confirms that the object, obj of Sub, is an instance of Sub, as well as it is an instance of Abst.
abstract void abstMthd();
}
class Sub extends Abst {
void abstMthd() {
System.out.println();
}
}
public class TheClass {
public static void main(String[] args) {
Sub obj = new Sub();
boolean bl = obj instanceofSub;
booleanbla = obj instanceofAbst;
System.out.println(bl);
System.out.println(bla);
}
}
The output is:
true
Implementation and Interface Type
In the following program, Impl is an implementation of the interface, Inter. The output confirms that the object, obj of Impl, is an instance of Impl, as well as it is an instance of Inter.
void abstMthd();
}
class Impl implements Inter {
public void abstMthd(){
System.out.println();
}
}
public class TheClass {
public static void main(String[] args) {
Impl obj = new Impl();
boolean bl = obj instanceofImpl;
booleanbli = obj instanceofInter;
System.out.println(bl);
System.out.println(bli);
}
}
The output is:
true
Type Variable
Type variable is codded in angle brackets. Consider the following program:
The output is: true. Note the way the vector coding has been done.
Conclusion
The instanceof operator checks if its operand on the left is an object of its operand on the right. If yes, it returns true, otherwise it returns false, or gives an error message at compile time. The operand on the left should be an instantiated object of the operand on the right. The operand on the right is a type, e.g. a class. instanceof is one of the relational operators in Java. The instanceof operator can be used in the if-condition.