Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Get the class of a variable

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
like image 851
Adam Matan Avatar asked Dec 13 '22 04:12

Adam Matan


1 Answers

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"
like image 83
aioobe Avatar answered Dec 29 '22 17:12

aioobe