Java中的instanceof运算符与isInstance()方法
instanceof运算符和isInstance()方法都用于检查对象的类。但主要区别在于,当我们想要动态检查对象的类时,isInstance() 方法将起作用。我们无法通过 instanceof运算符来做到这一点。
isInstance 方法等价于 instanceof运算符。该方法用于在运行时使用反射创建对象的情况。一般做法是,如果要在运行时检查类型,则使用 isInstance 方法,否则可以使用 instanceof运算符。
instanceof运算符和 isInstance() 方法都返回一个布尔值。 isInstance() 方法是Java中 Class 类的方法,而 instanceof 是运算符。
考虑一个例子:
Java
// Java program to demonstrate working of
// instanceof operator
public class Test
{
public static void main(String[] args)
{
Integer i = new Integer(5);
// prints true as i is instance of class
// Integer
System.out.println(i instanceof Integer);
}
}
Java
// Java program to demonstrate working of isInstance()
// method
public class Test {
// This method tells us whether the object is an
// instance of class whose name is passed as a
// string 'c'.
public static boolean fun(Object obj, String c)
throws ClassNotFoundException
{
return Class.forName(c).isInstance(obj);
}
// Driver code that calls fun()
public static void main(String[] args)
throws ClassNotFoundException
{
Integer i = new Integer(5);
// print true as i is instance of class
// Integer
boolean b = fun(i, "java.lang.Integer");
// print false as i is not instance of class
// String
boolean b1 = fun(i, "java.lang.String");
// print true as i is also instance of class
// Number as Integer class extends Number
// class
boolean b2 = fun(i, "java.lang.Number");
System.out.println(b);
System.out.println(b1);
System.out.println(b2);
}
}
Java
public class Test {
public static void main(String[] args)
{
Integer i = new Integer(5);
// Below line causes compile time error:-
// Incompatible conditional operand types
// Integer and String
System.out.println(i instanceof String);
}
}
输出:
true
现在如果我们想在运行时检查对象的类,那么我们必须使用isInstance()方法。
Java
// Java program to demonstrate working of isInstance()
// method
public class Test {
// This method tells us whether the object is an
// instance of class whose name is passed as a
// string 'c'.
public static boolean fun(Object obj, String c)
throws ClassNotFoundException
{
return Class.forName(c).isInstance(obj);
}
// Driver code that calls fun()
public static void main(String[] args)
throws ClassNotFoundException
{
Integer i = new Integer(5);
// print true as i is instance of class
// Integer
boolean b = fun(i, "java.lang.Integer");
// print false as i is not instance of class
// String
boolean b1 = fun(i, "java.lang.String");
// print true as i is also instance of class
// Number as Integer class extends Number
// class
boolean b2 = fun(i, "java.lang.Number");
System.out.println(b);
System.out.println(b1);
System.out.println(b2);
}
}
输出:
true
false
true
Note: instanceof operator throws compile-time error(Incompatible conditional operand types) if we check object with other classes which it doesn’t instantiate.
Java
public class Test {
public static void main(String[] args)
{
Integer i = new Integer(5);
// Below line causes compile time error:-
// Incompatible conditional operand types
// Integer and String
System.out.println(i instanceof String);
}
}
输出 :
9: error: incompatible types: Integer cannot be converted to String
System.out.println(i instanceof String);
^
必读:
- Java中的 new运算符与 newInstance() 方法
- Java中的反射