简体   繁体   English

为什么不按照它听起来的方式工作?

[英]why doesn't instanceof work the way it sounds?

In Java, the instanceof operator will only return false if comparing null . 在Java中, instanceof运算符仅在比较null时才返回false If you test a reference variable that isn't of the required type, the program fails to compile. 如果测试的引用变量不是所需类型,则程序无法编译。 Does this operator effectively only test if a reference is null or not? 此运算符是否仅有效地测试引用是否为null Or is there another use I'm not thinking of? 还是有其他用途我没想到的?

If not, why not call it isNull or something more descriptive? 如果没有,为什么不称它为isNull或更具描述性的东西?

I'm not sure how you came to your conclusion, but the instanceof operation will result in false when the runtime type of the first operand does not match the type specified by the second operand. 我不确定你是如何得出结论的,但是当第一个操作数的运行时类型与第二个操作数指定的类型不匹配时, instanceof操作将导致false

For example, 例如,

Object t1 = "Hello, World!";
System.out.println(t1 instanceof Number);

… prints "false" because t1 is a String , not a Number or one of its subclasses. ...打印“false”,因为t1是String ,而不是Number或其子类之一。

Can you please include your example of an instanceof operation that failed to compile? 你能否列举一个无法编译的操作instanceof的例子? Most likely, this is due to a syntax error. 最有可能的原因是语法错误。

It fails to compile if the comparison can never be true. 如果比较永远不会成立,则无法编译。 For example: 例如:

"string" instanceof Number

This will not compile because the compiler knows that a string can never be a number. 这将无法编译,因为编译器知道字符串永远不能是数字。 The operator does exactly what one would expect it to do: it tests if the operand is an instance of the specified type. 运算符完全按照人们的期望执行:它测试操作数是否是指定类型的实例。

Returning false when testing a null reference is meant to make the code simpler and more readable. 在测试空引用时返回false意味着使代码更简单,更易读。 It prevents you from needing to do this: 它可以防止您需要这样做:

if (obj != null && obj instanceof Number) {
    ...
}

Read this to learn about the operator: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2 阅读本文以了解运营商: http//java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2

It's useful when you have a superclass type reference and you want to check if the object is an instance of some subclass: 当你有一个超类类型引用并且想要检查该对象是否是某个子类的实例时,它很有用:

eg 例如

public class Sup { }

public class Sub1 extends Sup { }

public class Sub2 extends Sup { }

public void someMethod(Sup sup) {
    if(sup instanceof Sub1) {
        //....
    }
}

instanceof returns true when you use it agains an instance of the class at its right, or any derived from it. 当你再次使用它的一个类的实例,或者从它派生的任何实例时, instanceof返回true。 It will return false otherwise. 否则它将返回false。

class A { /*...*/ }
class B extends A { /*...*/ }

A objA = new A();
B objB = new B();

boolean test = objA instanceof A;   // test is true
test = objA instanceof B;   // test is false
test = objB instanceof A;   // test is true
test = objB instanceof B;   // test is true

The code above will not raise any exception. 上面的代码不会引发任何异常。 The following will do, however: 但是,以下情况可以:

objB = (B) objA;

Hope this helps. 希望这可以帮助。

if(MyObj instanceof SomeClass)
  • If left side objet is null= return false 如果左侧objet为null = return false
  • if left side is not instance of rigth side class then return false 如果左侧不是rigth side class的实例,则返回false
  • if left side is instance if right side then return true 如果左侧是实例,如果右侧则返回true

Please note, that instanceof usage can be avoided with polymorphism in most cases. 请注意,在大多数情况下,可以通过多态来避免使用instanceof。

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

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