For debugging purposes, I would like to display the type of a specific variable in Java, e.g.:
String s = "adasdas";
System.out.println( SOME_MAGIC_HERE(s) );
And get:
String
For debugging purposes, I would like to display the type of a specific variable in Java, e.g.:
String s = "adasdas";
System.out.println( SOME_MAGIC_HERE(s) );
And get:
String
You're looking for the Object.getClass() method.
Examples:
System.out.println(s.getClass()); // Prints "java.lang.String"
System.out.println(s.getClass().getSimpleName()); // Prints "String"
s.getClass() Class of a class: String.class Name of a class: clazz.getName()The following code will show the canonical name of the class and the Simple name of the class.
package com.personal.sof;
public class GetClassOfVariable {
public static void main(String[] args) {
String strVar = "Hello World";
System.out.println(strVar.getClass().getCanonicalName());
System.out.println(strVar.getClass().getSimpleName());
}
}
o/p :
java.lang.String
String