. Advertisement .
..3..
. Advertisement .
..4..
Getting the type of an object in Java is crucial when you need to process an array, for example. Your program can run into an error if you ignore this simple check. Follow the methods below to avoid those problems.
Find The Type Of An Object In Java
Using The instanceof Operator
You can use the “instanceof” operator to determine the type of an object in Java. This operator, also known as the type comparison operator, is useful when you need to explicitly test whether an object is an instance of a specified type, interface, or class.
Syntax:
<object> instanceof <type>
The operator needs two operands and returns a boolean value. If <object> is an instance of <type>, the expression yields true (and false if it is not).
Example:
public class Main {
public static void main(String[] args) {
// create a string
String site_name = "ITTutoria";
// check if the variable is a string
if (site_name instanceof String)
System.out.println(site_name + " is an instance of String.");
else
System.out.println(site_name + " is an instance of String.");
}
}
Output:
ITTutoria is an instance of String.
In this example, we integrate the result of the operator into an if-else statement to find out whether a variable has the string type.
Inherited classes are another application of the instanceof operator.
Example:
public class Main {
public static void main(String[] args) {
// create a superclass
class Smartphone {
}
// create a subclass
class Android extends Smartphone {
}
// create a object of the subclass
Android pixel = new Android();
// check whether pixel is an instance of the subclass
if (pixel instanceof Android)
System.out.println("pixel is an instance of Android");
else
System.out.println("pixel is not an instance of Android");
// check whether pixel is an instance of the superclass
if (pixel instanceof Smartphone)
System.out.println("pixel is an instance of Smartphone");
else
System.out.println("pixel is not an instance of Smartphone");
}
}
Output:
pixel is an instance of Android
pixel is an instance of Smartphone
The snippet creates a subclass of a superclass and then checks whether an object is an instance of those two classes.
Using The isInstance() MethodS
Similar to the instanceof operator, the isInstance() method can also be used to check whether an object is an instance of a type ( interface or class).
Example:
public class Main {
public static void main(String[] args) {
// create a variable
Long gdp = new Long(21433220);
// check if variables is a instance of the Long data type
if (Long.class.isInstance(gdp))
System.out.println("gdp is an instance of Long.");
else
System.out.println("gdp is not an instance of Long");
}
}
Output:
gdp is an instance of Long.
The above checks whether the gdp variable is of the long data type. Thanks to the boolean return value by the isInstance() method, you can also use it with an if-else statement.
Using getClass()
The getClass() method is a great choice if you don’t have any specific type or class in mind. It doesn’t take any parameter and returns the class of the object calling the method.
Example:
public class Main {
public static void main(String[] args) {
// check class of a string
String obj1 = new String("2022");
System.out.println("Class of obj1: " + obj1.getClass());
// check class of an integer
Integer obj2 = new Integer("2022");
System.out.println("Class of obj2: " + obj2.getClass());
}
}
Output:
Class of obj1: class java.lang.String
Class of obj2: class java.lang.Integer
Conclusion
There are plenty of ways to find out the type of an object in Java. If you want to check whether it is an instance of a provided type, both instanceof and isInstance() are handy options. On the other hand, the getClass() method gives you a more general tool for the job.
Leave a comment