get type java

https‮w//:‬ww.lautturi.com
get type java

To get the type of an object in Java, you can use the getClass() method, which is inherited from the Object class. This method returns an instance of the Class class, which represents the type of the object.

Here's an example of how you could use the getClass() method to get the type of an object:

Object obj = "hello";
Class cls = obj.getClass();
System.out.println("Type of object: " + cls.getName());

This code will output "Type of object: java.lang.String", because the object is a String.

You can also use the instanceof operator to check if an object is an instance of a specific type. For example:

if (obj instanceof String) {
  System.out.println("Object is a string");
} else if (obj instanceof Integer) {
  System.out.println("Object is an integer");
}

This code will output "Object is a string" because the object is a String.

Created Time:2017-11-01 12:04:59  Author:lautturi