How to find the data type of variables in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to find the data type of variables in java?

In python we have type function to print the data type. but incase of java, how to print the data type as output in simple way.. let suppose i have a string variable name... Eg.. String name ="Anima";

27th Oct 2021, 6:15 AM
Anima Singh Dhabal
Anima Singh Dhabal - avatar
4 Answers
+ 1
https://stackoverflow.com/questions/15770469/print-the-type-of-a-java-variable#:~:text=In%20Java%2C%20is%20it%20possible,String%22%20to%20the%20console.%20%7D You can read this. As it is not used in java as it is a statically typed language
27th Oct 2021, 6:45 AM
Atul [Inactive]
+ 1
String name = "Anima"; if (name instanceof String) System.out.println("name is String" ); if (String.class.isInstance(name) ) System.out.println("instance of String" ); Class<?> nameCl = name.getClass(); System.out.println( nameCl.getSimpleName() ); System.out.println( nameCl.getName() ); System.out.println( nameCl.getTypeName() ); System.out.println( nameCl.toString() ); System.out.println( nameCl );
27th Oct 2021, 10:54 AM
zemiak
+ 1
zemiak sorry but i didnt get your code..can you explain me what is this- Class<?>
27th Oct 2021, 11:49 AM
Anima Singh Dhabal
Anima Singh Dhabal - avatar
+ 1
Class<?> is type of class named 'Class' and it using one parameter of generic type instead ? can be eg String, but in this code we don't know what type there is so there is ? wildcard and java get the type from name variable
27th Oct 2021, 12:12 PM
zemiak