I just started working with java and I would like to know how I can know the type of a variable, if it is int, long, short, boolean, etc.
Thanks for the help!
I just started working with java and I would like to know how I can know the type of a variable, if it is int, long, short, boolean, etc.
Thanks for the help!
To know the type of a variable you can do it through the method
.getClass().getSimpleName()
(you can also use.getName()
) and casting(Object)
for the variable whose type you want to know.Example:
Departure:
If you already know the type of the variable and want to perform a comparison , you can use the instanceof type comparison operator
You can use instanceof to check the object it belongs to.
For example
int variable goes from: -2147486648 to 2147486647
Long variable goes from: 9223372036854775808 to 9223372036854775807
Short variable goes from: -32768 to 32767
Boolean: consists of 2 values true or false