How do you know a variable type in java? [duplicate]
Let's say I declare a variable:
String a = "test";
And I want to know what type it is, i.e., 开发者_如何学JAVAthe output should be java.lang.String
How do I do this?
a.getClass().getName()
Expanding on Martin's answer...
Martins Solution
a.getClass().getName()
Expanded Solution
If you want it to work with anything you can do this:
((Object) myVar).getClass().getName()
//OR
((Object) myInt).getClass().getSimpleName()
In case of a primitive type, it will be wrapped (Autoboxed) in a corresponding Object variant.
Example #1 (Regular)
private static String nameOf(Object o) {
return o.getClass().getSimpleName();
}
Example #2 (Generics)
public static <T> String nameOf(T o) {
return o.getClass().getSimpleName();
}
Additional Learning
- Material on Java Types, Values and Variables: https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html
- Autoboxing and Unboxing: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
- Docs on Pattern Matching for instanceof: https://docs.oracle.com/en/java/javase/14/language/pattern-matching-instanceof-operator.html
If you want the name, use Martin's method. If you want to know whether it's an instance of a certain class:
boolean b = a instanceof String
I learned from the Search Engine(My English is very bad , So code...) How to get variable's type? Up's :
String str = "test";
String type = str.getClass().getName();
value: type = java.lang.String
this method :
str.getClass().getSimpleName();
value:String
now example:
Object o = 1;
o.getClass().getSimpleName();
value:Integer
Use operator overloading feature of java
class Test {
void printType(String x) {
System.out.print("String");
}
void printType(int x) {
System.out.print("Int");
}
// same goes on with boolean,double,float,object ...
}
I think we have multiple solutions here:
- instance of could be a solution.
Why? In Java every class is inherited from the Object class itself. So if you have a variable and you would like to know its type. You can use
- System.out.println(((Object)f).getClass().getName());
or
- Integer.class.isInstance(1985); // gives true
or
isPrimitive()
public static void main(String[] args) { ClassDemo classOne = new ClassDemo(); Class classOneClass = classOne(); int i = 5; Class iClass = int.class; // checking for primitive type boolean retval1 = classOneClass.isPrimitive(); System.out.println("classOneClass is primitive type? = " + retval1); // checking for primitive type? boolean retval2 = iClass.isPrimitive(); System.out.println("iClass is primitive type? = " + retval2); }
This going to give us:
- FALSE
- TRUE
Find out more here: How to determine the primitive type of a primitive variable?
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
http://docs.oracle.com/cd/E26806_01/wlp.1034/e14255/com/bea/p13n/expression/operator/Instanceof.html
I agree with what Joachim Sauer said, not possible to know (the variable type! not value type!) unless your variable is a class attribute (and you would have to retrieve class fields, get the right field by name...)
Actually for me it's totally impossible that any a.xxx().yyy()
method give you the right answer since the answer would be different on the exact same object, according to the context in which you call this method...
As teehoo said, if you know at compile a defined list of types to test you can use instanceof but you will also get subclasses returning true...
One possible solution would also be to inspire yourself from the implementation of java.lang.reflect.Field
and create your own Field
class, and then declare all your local variables as this custom Field
implementation... but you'd better find another solution, i really wonder why you need the variable type, and not just the value type?
精彩评论