How do you know a variable type in java?

后端 未结 7 1704
野性不改
野性不改 2020-11-30 18:07

Let\'s say I declare a variable:

String a = \"test\";

And I want to know what type it is, i.e., the output should be java.lang.String

相关标签:
7条回答
  • 2020-11-30 18:36

    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
    
    0 讨论(0)
  • 2020-11-30 18:45
    a.getClass().getName()
    
    0 讨论(0)
  • 2020-11-30 19:01

    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

    0 讨论(0)
  • 2020-11-30 19:01

    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?

    0 讨论(0)
  • 2020-11-30 19:01

    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:

    1. FALSE
    2. 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

    0 讨论(0)
  • 2020-11-30 19:02

    I would like to expand on Martin's answer there...

    His solution is rather nice, but it can be tweaked so any "variable type" can be printed like that.(It's actually Value Type, more on the topic). That said, "tweaked" may be a strong word for this. Regardless, it may be helpful.

    Martins Solution:

    a.getClass().getName()
    

    However, If you want it to work with anything you can do this:

    ((Object) myVar).getClass().getName()
    //OR
    ((Object) myInt).getClass().getSimpleName()
    

    In this case, the primitive will simply be wrapped in a Wrapper. You will get the Object of the primitive in that case.

    I myself used it like this:

    private static String nameOf(Object o) {
        return o.getClass().getSimpleName();
    }
    

    Using Generics:

    public static <T> String nameOf(T o) {
        return o.getClass().getSimpleName();
    }
    
    0 讨论(0)
提交回复
热议问题