- Get Type of Object in Java
- Get Object Type Using getClass() in Java
- Get Object Type Using instanceOf in Java
- Get Type of a Manually Created Class Object in Java
- Related Article — Java Object
- how can I get the type of the object ?
- Java typeof Operator
- Get the Type of a Variable/Value in Java
- Get the Type of Any Variable/Value in Java
- Related Article — Java Operator
Get Type of Object in Java
- Get Object Type Using getClass() in Java
- Get Object Type Using instanceOf in Java
- Get Type of a Manually Created Class Object in Java
In this article, we’ll learn how to get the type of object in Java. It’s helpful to check the object type when the object comes from a source. It’s a place where we can’t verify the type of objects, such as from an API or a private class that we don’t have access to.
Get Object Type Using getClass() in Java
In the first method, we check the type of Object of wrapper classes like Integer and String . We have two objects, var1 and var2 , to check the type. We’ll use the getClass() method of the Object class, the parent class of all objects in Java.
We check the class using the if condition. As the wrapper classes also contain a field class that returns the type, we can check whose type matches with var1 and var2 . Below, we check both the objects with three types.
public class ObjectType public static void main(String[] args) Object var1 = Integer.valueOf("15"); Object var2 = String.valueOf(var1); if (var1.getClass() == Integer.class) System.out.println("var1 is an Integer"); > else if (var1.getClass() == String.class) System.out.println("var1 is a String"); > else if (var1.getClass() == Double.class) System.out.println("var1 is a Double"); > if (var2.getClass() == Integer.class) System.out.println("var2 is an Integer"); > else if (var2.getClass() == String.class) System.out.println("var2 is a String"); > else if (var2.getClass() == Double.class) System.out.println("var2 is a Double"); > > >
var1 is an Integer var2 is a String
Get Object Type Using instanceOf in Java
Another method to get the type of object in Java is by using the instanceOf function; it returns if the object’s instance matches with the given class. In this example, we have the objects var1 and var2 that are checked with these three types: Integer , String , and Double ; if any of the conditions meet, we can execute a different code.
Because var1 is of type Integer , the condition var1 instanceOf Integer will become true, and var2 is Double so, var2 instanceOf Double will become true.
public class ObjectType public static void main(String[] args) Object var1 = Integer.valueOf("15"); Object var2 = Double.valueOf("10"); if (var1 instanceof Integer) System.out.println("var1 is an Integer"); > else if (var1 instanceof String) System.out.println("var1 is a String"); > else if (var1 instanceof Double) System.out.println("var1 is a Double"); > if (var2 instanceof Integer) System.out.println("var2 is an Integer"); > else if (var2 instanceof String) System.out.println("var2 is a String"); > else if (var2 instanceof Double) System.out.println("var2 is a Double"); > > >
var1 is an Integer var2 is a Double
Get Type of a Manually Created Class Object in Java
We checked the types of wrapper classes, but in this example, we get the type of three objects of three manually-created classes. We create three classes: ObjectType2 , ObjectType3 and ObjectType4 .
ObjectType3 inherits ObjectType4 , and ObjectType2 inherits ObjectType3 . Now we want to know the type of objects of all these classes. We have three objects, obj , obj2 , and obj3 ; we use both the methods that we discussed in the above examples that are getClass() and instanceOf .
However, there differences between the type of obj2 . The obj2 variable returned the type ObjectType4 while its class is ObjectType3 . It happens because we inherit the ObjectType4 class in the ObjectType3 and the instanceOf checks all the classes and subclasses. The obj returned ObjectType3 because the getClass() function checks only the direct class.
public class ObjectType public static void main(String[] args) Object obj = new ObjectType2(); Object obj2 = new ObjectType3(); Object obj3 = new ObjectType4(); if (obj.getClass() == ObjectType4.class) System.out.println("obj is of type ObjectType4"); > else if (obj.getClass() == ObjectType3.class) System.out.println("obj is of type ObjectType3"); > else if (obj.getClass() == ObjectType2.class) System.out.println("obj is of type ObjectType2"); > if (obj2 instanceof ObjectType4) System.out.println("obj2 is an instance of ObjectType4"); > else if (obj2 instanceof ObjectType3) System.out.println("obj2 is an instance of ObjectType3"); > else if (obj2 instanceof ObjectType2) System.out.println("obj2 is an instance of ObjectType2"); > if (obj3 instanceof ObjectType4) System.out.println("obj3 is an instance of ObjectType4"); > else if (obj3 instanceof ObjectType3) System.out.println("obj3 is an instance of ObjectType3"); > else if (obj3 instanceof ObjectType2) System.out.println("obj3 is an instance of ObjectType2"); > > > class ObjectType2 extends ObjectType3 int getAValue3() System.out.println(getAValue2()); a = 300; return a; > > class ObjectType3 extends ObjectType4 int getAValue2() System.out.println(getAValue1()); a = 200; return a; > > class ObjectType4 int a = 50; int getAValue1() a = 100; return a; > >
obj is of type ObjectType2 obj2 is an instance of ObjectType4 obj3 is an instance of ObjectType4
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
Related Article — Java Object
Copyright © 2023. All right reserved
how can I get the type of the object ?
posted 16 years ago
- 1
hello
if I have some variable and I like to know what is the stype of this variable
if there any «typeOf» method that gives me that info?
posted 16 years ago
- 1
MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress — URLybird 1.2.1)
Java Cowboy
posted 16 years ago
- 2
You can use getClass() to get the java.lang.Class object that represents the type of the object.
If you just want to know if an object is an instance of or extends a certain class, or implements a certain interface, you can use the instanceof keyword.
posted 16 years ago
- 1
The above answers (suggesting getClass() and instanceof) are quite correct.
But, as this is the beginner forum, I thought I’d add a comment on use and misuse of these features.
Java is an object-oriented language (some would say not 100% so, but. ). One therefore has powerful features like polymorphism available, to make objects of different types behave differently when sent the same message (method call). One should use those object-oriented features as much as possible.
It is usually bad practice to write code that conditionalises (if, switch etc.) on the type of an object. As much as possible, this should be replaced by something more object-oriented.
It’s not a hard-and-fast rule. Although some Ranchers may disagree, I favour pragmatism. If a particular problem can be solved much quicker by using instanceof than by refactoring to use polymorphism, then it may be OK to use instanceof. But don’t get too much into the habit.
If you were thinking of using getClass() or instanceof in an «if» or «switch» statement, you should probably go back and revisit your design.
Betty Rubble? Well, I would go with Betty. but I’d be thinking of Wilma.
Java typeof Operator
- Get the Type of a Variable/Value in Java
- Get the Type of Any Variable/Value in Java
This tutorial introduces how to get the data type of a variable or value in Java and lists some example codes to understand the topic.
In Java, to get type of a variable or a value, we can use getClass() method of Object class. This is the only way to do this, unlike JavaScript with the typeof() method to check type.
Since we used the getClass() method of Object class, it works with objects only, not primitives. If you want to get the type of primitives, then first convert them using the wrapper class. Let’s understand with some examples.
Get the Type of a Variable/Value in Java
In this example, we used getClass() to check the type of a variable. Since this variable is a string type, then we can directly call the method. See the example below.
Notice that the getClass() method returns a fully qualified class name, including a package name such as java.lang.String in our case.
public class SimpleTesting public static void main(String[] args) String msg = "Hello"; System.out.println(msg); System.out.println("Type is: "+msg.getClass()); > >
Hello Type is: class java.lang.String
Get the Type of Any Variable/Value in Java
In the above example, we used a string variable and got its type similarly; we can also use another type of variable, and the method returns the desired result. See the example below.
In this example, we created two more variables, integer and character, apart from string and used the getClass() method.
package javaexample; public class SimpleTesting public static void main(String[] args) String msg = "Hello"; System.out.println(msg); System.out.println("Type is: "+msg.getClass()); // Integer Integer val = 20; System.out.println(val); System.out.println("Type is: "+val.getClass()); // Character Character ch = 'G'; System.out.println(ch); System.out.println("Type is: "+ch.getClass()); > >
Hello Type is: class java.lang.String 20 Type is: class java.lang.Integer G Type is: class java.lang.Character
The getClass() method returns a complete qualified name of the class, including the package name. If you wish to get only the type name, you can use the getSimpleName() method that returns a single string. See the example below.
package javaexample; public class SimpleTesting public static void main(String[] args) String msg = "Hello"; System.out.println(msg); System.out.println("Type is: "+msg.getClass().getSimpleName()); // Integer Integer val = 20; System.out.println(val); System.out.println("Type is: "+val.getClass().getSimpleName()); // Character Character ch = 'G'; System.out.println(ch); System.out.println("Type is: "+ch.getClass().getSimpleName()); > >
Hello Type is: String 20 Type is: Integer G Type is: Character