简体   繁体   English

Java中的变量类型

[英]types of variable in java

Is there a way to get the type of variable in java ? 有没有一种方法来获取Java中变量的类型 ? my goal is to define If the variable is int or String 我的目标是定义变量是int还是String

There is a way I tried to use before but it didn't work with int . 我以前曾经尝试过一种方法,但是int不能用。 It is just for String 它仅适用于String

This is code : 这是代码:

String a="121212aa";
System.out.println(a.getClass().isInstance(a));

a now is String and the result will be java.lang.String . a是现在的字符串并将结果将是java.lang.String

i need to check also if it's int because if I use Boolean with else in it maybe the input is double 我还需要检查它是否为int因为如果我在其他情况下使用Boolean ,则输入可能为double

In Java there are primitive and reference types. 在Java中,有原始类型和引用类型。

Primitive types are boxed into wrapper types. 原始类型装箱成包装类型。 You should check against the proper wrapper type , eg: 您应该检查正确的包装器类型 ,例如:

Object i = 1;
i.getClass(); // will return java.lang.Integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM