Are you new to Java Object Oriented programming and looking for solutions to check the type of object in Java?
In this article, we discuss what an object is. How can we create an object, how can we check an object type and the use of the getClass() method and instanceof() operators. Now, let’s answer these questions one by one.
Table of Contents
What is an Object in Java?
In object-oriented programming (OOP), objects are the entity you think about in developing a program. As we all know, Java is an object-oriented programming language. Everything in Java is an object; it is the core concept of this Language.
We create an object of the class. A class is just a template or prototype of a real-world entity. Whereas objects represent a real-world entity, they occupy memory.
How to Create an Object in Java?
In Java, every object must have a unique name and consist of attributes and methods.
We can create an object of the class with the help of a new keyword:
Employee emp = new Employee();
In this example, we declare emp an instance of class Employee.
How to “Check The Type of an Object” in Java?
We can check the user-defined or predefined types of an object. We have two ways to check the type of an object in Java:
- The getClass() method
- The instanceof operator
- The typeof operator
- The Object.prototype.toString.call() method
Now let’s discuss both ways one by one and explore when we use them.
Method 1: Using getClass()
The java.lang.Object class is the root of all classes, so all the classes have an Object as a superclass. One of the most useful methods of Object class is getClass(); this method will return the runtime class of the object. You can then use the `isInstance()` method of the `Class` object to check if the object is an instance of a specific class or interface.
In Java, we have predefined classes such as String, Double, Integer, and many more. Sometimes we need to confirm the object type while using predefined classes. For this, we have to use getClass() method that belongs to the Object class.
The general Syntax of getClass():
public final Classl<?> getClass()
Now understand this method with the help of an example:
Code
package org.example; public class Main { public static void main(String[] args) { // Object of Main class Main a= new Main(); // x, y and z object of wrapper classes for the primitive types String x= "getclass example"; Integer y= 20; Double z=20.20; // with help of getClass method get the name of object class System.out.println("a is object of class"); System.out.println(a.getClass()); System.out.println("x is object of class"); System.out.println(x.getClass()); System.out.println("y is object of class"); System.out.println(y.getClass()); System.out.println("z is object of class"); System.out.println(z.getClass()); } }
Output
a is object of class class org.example.Main x is object of class class java.lang.String y is object of class class java.lang.Integer z is object of class class java.lang.Double
With the help of this example, we see how to check the type of object. getClass() method returns the name of the class.
By using the `isInstance()` method of the `Class` object, you can check the type of an object in a more flexible way than with the `instanceof` operator. This can be useful if you need to perform different actions based on the type of an object, or if you need to check the type of an object for other purposes.
Method 2: Using Instanceof
We can check the object type with the help of an instanceof operator, also known as a type comparison operator. Determining object type is essential when handling more than one object of different types. Instanceof operator returns a boolean, either true or false. If we initialize an object with null and check instanceof, it will return false. This operator returns true
if the object is an instance of the specified class or interface, and false
if it is not.
Code
package org.example; public class Main { public static void main(String[] args) { // Object of Main class Main a= new Main(); // x, y and z object of wrapper classes for the primitive types String x= "instanceof operator example"; Integer y= 20; Double z=20.20; // with help of instanceof operator confirm the name of object class System.out.println("Is a object of class Main"); System.out.println(a instanceof Main); System.out.println("Is x object of class String"); System.out.println(x instanceof String); System.out.println("Is y object of class Integer"); System.out.println(y instanceof Integer); System.out.println("Is z object of class Double"); System.out.println(z instanceof Double); } }
Output
Is a object of class Main true Is x object of class String true Is y object of class Integer true Is z object of class Double true
Now let’s see if we initialize an object with null and then check its type:
Code
package org.example; public class Main { public static void main(String[] args) { // Object of Main class Main a= null; // with help of instanceof operator confirm the name of class object System.out.println("Is a object of class Main"); System.out.println(a instanceof Main); } }
Output
Is a object of class Main false
By using the instanceof
operator, you can easily check the type of an object in Java. This can be useful if you need to perform different actions based on the type of an object, or if you need to check the type of an object for other purposes.
It’s worth noting that the instanceof
operator only works with reference types, not with primitive types. If you need to check the type of a primitive value, you can use the typeof
operator in JavaScript, but this operator is not available in Java. Instead, you can use the Type
class from the java.lang
package to check the type of a primitive value.
For example, here’s how you can use the Type
class to check the type of an int
value:
int value = 42; if (Type.INT.equals(value.getClass())) { System.out.println("value is an int"); } else { System.out.println("value is not an int"); } // Output: value is an int
This code defines an int
variable called value
and assigns it the value 42. The Type.INT
constant is then used to check if the class of value
is equal to int.class
. Since it is, the code prints “value is an int”.
You can use the Type
class to check the type of other primitive values as well, such as boolean
, char
, byte
, short
, long
, float
, and double
. Here’s an example of how to check the type of a double
value:
double value = 3.14; if (Type.DOUBLE.equals(value.getClass())) { System.out.println("value is a double");} else { System.out.println("value is not a double"); }
// Output: value is a double
Method 3: Using typeof
It’s also worth noting that you can use the typeof
operator in JavaScript to check the type of a value, whether it is a primitive value or an object. The typeof
operator returns a string that represents the type of the value, such as “number”, “string”, “boolean”, or “object”. Here’s an example of how to use the typeof
operator:
let value = 42; console.log(typeof value); // Output: "number" value = 'Hello, World!'; console.log(typeof value); // Output: "string" value = true; console.log(typeof value); // Output: "boolean" value = { name: 'John' }; console.log(typeof value); // Output: "object" value = null; console.log(typeof value); // Output: "object"
This code defines a variable called value
and assigns it different values of different types. The typeof
operator is then used to print the type of each value to the console. As you can see, the typeof
operator returns the expected string for each value.
It’s worth noting that the typeof
operator returns “object” for objects and arrays, and “null” for null values. If you need to distinguish between different types of objects or arrays, you can use the instanceof
operator or the constructor
property, as described in the previous examples.
In conclusion, the typeof operator is a useful way to check the type of a value in JavaScript, whether it is a primitive value or an object. This can be useful if you need to perform different actions based on the type of a value, or if you need to check the type of a value for other purposes. You can use the instanceof operator or the `construct
In conclusion, the typeof
operator is a useful way to check the type of a value in JavaScript, whether it is a primitive value or an object. This can be useful if you need to perform different actions based on the type of a value, or if you need to check the type of a value for other purposes. You can use the instanceof
operator or the `constructor` property to further distinguish between different types of objects or arrays if necessary.
Method 4: Using Object.prototype.toString.call()
It’s also worth noting that you can use the Object.prototype.toString.call()
method to get a string representation of the type of an object in JavaScript. This method is a way to access the toString()
method of an object’s prototype, allowing you to pass an object as an argument to the method.
Here’s an example of how to use the Object.prototype.toString.call()
method:
let value = 42; console.log(Object.prototype.toString.call(value)); // Output: "[object Number]" value = 'Hello, World!'; console.log(Object.prototype.toString.call(value)); // Output: "[object String]" value = true; console.log(Object.prototype.toString.call(value)); // Output: "[object Boolean]" value = { name: 'John' }; console.log(Object.prototype.toString.call(value)); // Output: "[object Object]" value = null; console.log(Object.prototype.toString.call(value)); // Output: "[object Null]" value = [1, 2, 3]; console.log(Object.prototype.toString.call(value)); // Output: "[object Array]"
This code defines a variable called value
and assigns it different values of different types. The Object.prototype.toString.call()
method is then used to print the string representation of each value type to the console. As you can see, the method returns the expected string for each value.
By using the Object.prototype.toString.call()
method, you can get a string representation of the type of an object in JavaScript. This can be useful if you need to perform different actions based on the type of an object, or if you need to check the type of an object for other purposes. You can use string manipulation techniques, such as substring()
or slice()
, to extract the type from the string if necessary.
It’s worth noting that the Object.prototype.toString.call()
method is a relatively advanced technique, and it may not be suitable for all situations. In many cases, the typeof
operator or the instanceof
operator may be sufficient for checking the type of a value in JavaScript.
Conclusion
As debated; we have two ways to check the object type in Java. We use getClass() whenever we want to know the name of the class to which an object belongs, and if we are going to check the object belonging to the given class, then we use the instanceof operator. This operator returns true or false.
In conclusion, there are several different ways to check the type of an object in Java. You can use the instanceof
operator to check if an object is an instance of a specific class or interface, or you can use the Type
class and the getClass()
method to check the type of a primitive value. You can also use the getClass()
method and the isInstance()
method of the Class
object to check the type of an object in a more flexible way. Regardless of which method you choose, you should now better understand how to check the type of an object in Java.
Let’s have a quick review of the topics discussed in this article.
- What is an object in Java?
- How to declare an object in Java?
- How can we check object type in Java?
- Use of getClass() method.
- Use of instanceof operator.
- Use of typeof operator.
- Use of Object.prototype.toString.call() operator
If you’ve found this article beneficial, don’t forget to share it with your coding fellows. Also, comment below 👇 which solution is more suitable in your case.
Happy coding!🥳