How can I check to make sure my variable is an int, array, double, etc...?
我如何检查以确保我的变量是int,array,double等......?
Edit: For example, how can I check that a variable is an array? Is there some function to do this?
编辑:例如,我如何检查变量是否为数组?有这样的功能吗?
9 个解决方案
#1
81
Java is a statically typed language, so the compiler does most of this checking for you. Once you declare a variable to be a certain type, the compiler will ensure that it is only ever assigned values of that type (or values that are sub-types of that type).
Java是一种静态类型语言,因此编译器会为您完成大部分检查。将变量声明为某种类型后,编译器将确保只为该类型赋值(或者是该类型的子类型的值)。
The examples you gave (int, array, double) these are all primitives, and there are no sub-types of them. Thus, if you declare a variable to be an int
:
您给出的示例(int,array,double)这些都是基元,并且没有它们的子类型。因此,如果将变量声明为int:
int x;
You can be sure it will only ever hold int
values.
您可以确定它只会保存int值。
If you declared a variable to be a List
, however, it is possible that the variable will hold sub-types of List
. Examples of these include ArrayList
, LinkedList
, etc.
但是,如果将变量声明为List,则变量可能包含List的子类型。这些示例包括ArrayList,LinkedList等。
If you did have a List
variable, and you needed to know if it was an ArrayList
, you could do the following:
如果你有一个List变量,并且你需要知道它是否是一个ArrayList,你可以执行以下操作:
List y;
...
if (y instanceof ArrayList) {
...its and ArrayList...
}
However, if you find yourself thinking you need to do that, you may want to re-think your approach. In most cases, if you follow object-oriented principals, you will not need to do this. There are, of course, exceptions to every rule, though.
但是,如果您发现自己认为需要这样做,则可能需要重新考虑自己的方法。在大多数情况下,如果您遵循面向对象的主体,则不需要执行此操作。当然,每条规则都有例外。
#2
31
Actually quite easy to roll your own tester, by abusing Java's method overload ability. Though I'm still curious if there is an official method in the sdk.
实际上很容易通过滥用Java的方法过载能力来推动自己的测试人员。虽然我仍然很好奇,如果在sdk中有官方方法。
Example:
例:
class Typetester {
void printType(byte x) {
System.out.println(x + " is an byte");
}
void printType(int x) {
System.out.println(x + " is an int");
}
void printType(float x) {
System.out.println(x + " is an float");
}
void printType(double x) {
System.out.println(x + " is an double");
}
void printType(char x) {
System.out.println(x + " is an char");
}
}
then:
然后:
Typetester t = new Typetester();
t.printType( yourVariable );
#3
16
a.getClass().getName()
- will give you the datatype of the actual object referred to by a
, but not the datatype that the variable a
was originally declared as or subsequently cast to.
a.getClass()。getName() - 将为您提供a引用的实际对象的数据类型,但不提供变量a最初声明或随后转换为的数据类型。
boolean b = a instanceof String
- will give you whether or not the actual object referred to by a
is an instance of a specific class. Again, the datatype that the variable a
was originally declared as or subsequently cast to has no bearing on the result of the instanceof operator.
boolean b =一个instanceof String - 将为您提供a引用的实际对象是否是特定类的实例。同样,变量a最初声明或随后转换为的数据类型与instanceof运算符的结果无关。
I took this information from: How do you know a variable type in java?
我从以下信息中获取了这些信息:您如何知道java中的变量类型?
This can happen. I'm trying to parse a String
into an int and I'd like to know if my Integer.parseInt(s.substring(a, b))
is kicking out an int or garbage before I try to sum it up.
这可能发生。我正在尝试将一个String解析为一个int,我想知道我的Integer.parseInt(s.substring(a,b))是否在我尝试总结之前踢出一个int或者垃圾。
By the way, this is known as Reflection. Here's some more information on the subject: http://docs.oracle.com/javase/tutorial/reflect/
顺便说一句,这被称为反射。以下是有关该主题的更多信息:http://docs.oracle.com/javase/tutorial/reflect/
#4
8
You may work with Integer instead of int, Double instead of double, etc. (such classes exists for all primitive types). Then you may use the operator instanceof, like if(var instanceof Integer){...}
您可以使用Integer而不是int,Double而不是double等(这些类适用于所有基本类型)。然后你可以使用运算符instanceof,比如if(var instanceof Integer){...}
#5
6
The first part of your question is meaningless. There is no circumstance in which you don't know the type of a primitive variable at compile time.
你问题的第一部分毫无意义。在编译时,您不会知道原始变量的类型。
Re the second part, the only circumstance that you don't already know whether a variable is an array is if it is an Object. In which case object.getClass().isArray()
will tell you.
在第二部分中,唯一没有知道变量是否是数组的情况是它是否是一个Object。在这种情况下,object.getClass()。isArray()会告诉你。
#6
5
I did it using: if(x.getClass() == MyClass.class){...}
我用它做了:if(x.getClass()== MyClass.class){...}
#7
5
Well, I think checking the type of variable can be done this way.
好吧,我认为检查变量的类型可以这样做。
public <T extends Object> void checkType(T object) {
if (object instanceof Integer)
System.out.println("Integer ");
else if(object instanceof Double)
System.out.println("Double ");
else if(object instanceof Float)
System.out.println("Float : ");
else if(object instanceof List)
System.out.println("List! ");
else if(object instanceof Set)
System.out.println("Set! ");
}
This way you need not have multiple overloaded methods. I think it is good practice to use collections over arrays due to the added benefits. Having said that, I do not know how to check for an array type. Maybe someone can improve this solution. Hope this helps!
这样您就不需要多个重载方法。我认为由于额外的好处,使用集合而不是阵列是一种好习惯。话虽如此,我不知道如何检查数组类型。也许有人可以改进这个解决方案希望这可以帮助!
P.S Yes, I know that this doesn't check for primitives as well.
P.S是的,我知道这也不会检查原语。
#8
1
I wasn't happy with any of these answers, and the one that's right has no explanation and negative votes so I searched around, found some stuff and edited it so that it is easy to understand. Have a play with it, not as straight forward as one would hope.
我对这些答案中的任何一个都不满意,而那个正确的答案没有解释和反对票,所以我搜索了一下,发现了一些东西并编辑了它以便它易于理解。玩弄它,而不是像人们希望的那样直截了当。
//move your variable into an Object type
Object obj=whatYouAreChecking;
System.out.println(obj);
// moving the class type into a Class variable
Class cls=obj.getClass();
System.out.println(cls);
// convert that Class Variable to a neat String
String answer = cls.getSimpleName();
System.out.println(answer);
Here is a method:
这是一个方法:
public static void checkClass (Object obj) {
Class cls = obj.getClass();
System.out.println("The type of the object is: " + cls.getSimpleName());
}
#9
-3
public class Demo1 {
公共课Demo1 {
Object printType(Object o)
{
return o;
}
public static void main(String[] args) {
Demo1 d=new Demo1();
Object o1=d.printType('C');
System.out.println(o1.getClass().getSimpleName());
}
}
}
#1
81
Java is a statically typed language, so the compiler does most of this checking for you. Once you declare a variable to be a certain type, the compiler will ensure that it is only ever assigned values of that type (or values that are sub-types of that type).
Java是一种静态类型语言,因此编译器会为您完成大部分检查。将变量声明为某种类型后,编译器将确保只为该类型赋值(或者是该类型的子类型的值)。
The examples you gave (int, array, double) these are all primitives, and there are no sub-types of them. Thus, if you declare a variable to be an int
:
您给出的示例(int,array,double)这些都是基元,并且没有它们的子类型。因此,如果将变量声明为int:
int x;
You can be sure it will only ever hold int
values.
您可以确定它只会保存int值。
If you declared a variable to be a List
, however, it is possible that the variable will hold sub-types of List
. Examples of these include ArrayList
, LinkedList
, etc.
但是,如果将变量声明为List,则变量可能包含List的子类型。这些示例包括ArrayList,LinkedList等。
If you did have a List
variable, and you needed to know if it was an ArrayList
, you could do the following:
如果你有一个List变量,并且你需要知道它是否是一个ArrayList,你可以执行以下操作:
List y;
...
if (y instanceof ArrayList) {
...its and ArrayList...
}
However, if you find yourself thinking you need to do that, you may want to re-think your approach. In most cases, if you follow object-oriented principals, you will not need to do this. There are, of course, exceptions to every rule, though.
但是,如果您发现自己认为需要这样做,则可能需要重新考虑自己的方法。在大多数情况下,如果您遵循面向对象的主体,则不需要执行此操作。当然,每条规则都有例外。
#2
31
Actually quite easy to roll your own tester, by abusing Java's method overload ability. Though I'm still curious if there is an official method in the sdk.
实际上很容易通过滥用Java的方法过载能力来推动自己的测试人员。虽然我仍然很好奇,如果在sdk中有官方方法。
Example:
例:
class Typetester {
void printType(byte x) {
System.out.println(x + " is an byte");
}
void printType(int x) {
System.out.println(x + " is an int");
}
void printType(float x) {
System.out.println(x + " is an float");
}
void printType(double x) {
System.out.println(x + " is an double");
}
void printType(char x) {
System.out.println(x + " is an char");
}
}
then:
然后:
Typetester t = new Typetester();
t.printType( yourVariable );
#3
16
a.getClass().getName()
- will give you the datatype of the actual object referred to by a
, but not the datatype that the variable a
was originally declared as or subsequently cast to.
a.getClass()。getName() - 将为您提供a引用的实际对象的数据类型,但不提供变量a最初声明或随后转换为的数据类型。
boolean b = a instanceof String
- will give you whether or not the actual object referred to by a
is an instance of a specific class. Again, the datatype that the variable a
was originally declared as or subsequently cast to has no bearing on the result of the instanceof operator.
boolean b =一个instanceof String - 将为您提供a引用的实际对象是否是特定类的实例。同样,变量a最初声明或随后转换为的数据类型与instanceof运算符的结果无关。
I took this information from: How do you know a variable type in java?
我从以下信息中获取了这些信息:您如何知道java中的变量类型?
This can happen. I'm trying to parse a String
into an int and I'd like to know if my Integer.parseInt(s.substring(a, b))
is kicking out an int or garbage before I try to sum it up.
这可能发生。我正在尝试将一个String解析为一个int,我想知道我的Integer.parseInt(s.substring(a,b))是否在我尝试总结之前踢出一个int或者垃圾。
By the way, this is known as Reflection. Here's some more information on the subject: http://docs.oracle.com/javase/tutorial/reflect/
顺便说一句,这被称为反射。以下是有关该主题的更多信息:http://docs.oracle.com/javase/tutorial/reflect/
#4
8
You may work with Integer instead of int, Double instead of double, etc. (such classes exists for all primitive types). Then you may use the operator instanceof, like if(var instanceof Integer){...}
您可以使用Integer而不是int,Double而不是double等(这些类适用于所有基本类型)。然后你可以使用运算符instanceof,比如if(var instanceof Integer){...}
#5
6
The first part of your question is meaningless. There is no circumstance in which you don't know the type of a primitive variable at compile time.
你问题的第一部分毫无意义。在编译时,您不会知道原始变量的类型。
Re the second part, the only circumstance that you don't already know whether a variable is an array is if it is an Object. In which case object.getClass().isArray()
will tell you.
在第二部分中,唯一没有知道变量是否是数组的情况是它是否是一个Object。在这种情况下,object.getClass()。isArray()会告诉你。
#6
5
I did it using: if(x.getClass() == MyClass.class){...}
我用它做了:if(x.getClass()== MyClass.class){...}
#7
5
Well, I think checking the type of variable can be done this way.
好吧,我认为检查变量的类型可以这样做。
public <T extends Object> void checkType(T object) {
if (object instanceof Integer)
System.out.println("Integer ");
else if(object instanceof Double)
System.out.println("Double ");
else if(object instanceof Float)
System.out.println("Float : ");
else if(object instanceof List)
System.out.println("List! ");
else if(object instanceof Set)
System.out.println("Set! ");
}
This way you need not have multiple overloaded methods. I think it is good practice to use collections over arrays due to the added benefits. Having said that, I do not know how to check for an array type. Maybe someone can improve this solution. Hope this helps!
这样您就不需要多个重载方法。我认为由于额外的好处,使用集合而不是阵列是一种好习惯。话虽如此,我不知道如何检查数组类型。也许有人可以改进这个解决方案希望这可以帮助!
P.S Yes, I know that this doesn't check for primitives as well.
P.S是的,我知道这也不会检查原语。
#8
1
I wasn't happy with any of these answers, and the one that's right has no explanation and negative votes so I searched around, found some stuff and edited it so that it is easy to understand. Have a play with it, not as straight forward as one would hope.
我对这些答案中的任何一个都不满意,而那个正确的答案没有解释和反对票,所以我搜索了一下,发现了一些东西并编辑了它以便它易于理解。玩弄它,而不是像人们希望的那样直截了当。
//move your variable into an Object type
Object obj=whatYouAreChecking;
System.out.println(obj);
// moving the class type into a Class variable
Class cls=obj.getClass();
System.out.println(cls);
// convert that Class Variable to a neat String
String answer = cls.getSimpleName();
System.out.println(answer);
Here is a method:
这是一个方法:
public static void checkClass (Object obj) {
Class cls = obj.getClass();
System.out.println("The type of the object is: " + cls.getSimpleName());
}
#9
-3
public class Demo1 {
公共课Demo1 {
Object printType(Object o)
{
return o;
}
public static void main(String[] args) {
Demo1 d=new Demo1();
Object o1=d.printType('C');
System.out.println(o1.getClass().getSimpleName());
}
}
}