+ 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";
4 ответов
+ 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
+ 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 );
+ 1
zemiak sorry but i didnt get your code..can you explain me what is this- Class<?>
+ 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