How to know datatype of a variable in Java ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to know datatype of a variable in Java ?

In python we have type() function but how can i know the datatype of variable in Java?

18th Oct 2020, 4:27 PM
Lakshay Deshraj
2 Answers
+ 3
Depends what you exactly mean by a variable. But for an object it should be getName and getClass. Also there is something called instanceOf which I think for the array list
18th Oct 2020, 4:34 PM
Khaled ^^ خالد القريشي‎
Khaled ^^ خالد القريشي‎ - avatar
+ 3
You can find the type only for reference type variables. String str = "hello"; System.out.println(str instanceof String); OR System.out.println( str.getClass().getName());
18th Oct 2020, 4:40 PM
Avinesh
Avinesh - avatar