Java Data Type Check: Complete Guide
In Java, you can use the getClass() method or the instanceof keyword to check the type of data.
- Get the class.
Object obj = new String("Hello");
Class objClass = obj.getClass();
System.out.println("数据类型为:" + objClass.getName());
- identical to
Object obj = new String("Hello");
if (obj instanceof String) {
System.out.println("数据类型为String");
} else {
System.out.println("数据类型不是String");
}
Both of these methods can help us to view the type of data.