How do you know a variable type in java?

后端 未结 7 1754
野性不改
野性不改 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
    

提交回复
热议问题