Answers for "java how can we print out an Object that we don't know the type"

0

java print type of object

//Example:
Integer number = 15;
System.out.println(number.getClass().getName());

//This print to console the fully qualified name of the class, which for the example is:
java.lang.Integer

//If you want a more concise output, you can use instead:
System.out.println(number.getClass().getSimpleName());

//getSimpleName() give you only the name of the class:
Integer

//Printing the type of primitive variables is a bit more complex: see this 
//https://stackoverflow.com/questions/180097/dynamically-find-the-class-that-represents-a-primitive-java-type
//for details.
Posted by: Guest on July-01-2020

Code answers related to "java how can we print out an Object that we don't know the type"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language