简体   繁体   English

为什么或为什么不使用instanceof检查是否不为null

[英]Why or why not use instanceof to check if not null

I just wonder if there is a good reason or practice of when to use != null instead of instanceof to check if something is null. 我只是想知道何时使用!= null而不是instanceof来检查某项是否为null是否有充分的理由或做法。

Is a bad practice to test if something is null with instanceof ? 测试instanceof是否为空是一种不好的做法吗?

For example: 例如:

View view = ((Activity) context).findViewById(viewID);
                                if (view instanceof  View) {
                                    listener.onView(view, viewID);
                                }

or 要么

View view = ((Activity) context).findViewById(viewID);
                            if (view != null) {
                                listener.onView(view, viewID);
                            }

Shouldn't it works just as same? 它不应该一样工作吗?

That depends on what you are actually checking 这取决于您实际检查的内容

  • If you just want to know whether the reference is null , use x == null and nothing else 如果您只想知道引用是否为null ,请使用x == null ,不要使用其他任何东西
  • If you want to know whether a particular reference is not null and points to an instance of type Foo you may use x instanceof Foo as it implies being not null 如果您想知道特定的引用是否不为null并指向Foo类型的实例,则可以使用x instanceof Foo因为它暗示不为null

    But if the compile-time type of the reference is already Foo , you know that the instance is of type Foo when it is non- null , therefore the first bullet applies in this case; 但是,如果引用的编译时类型已经是Foo ,你就知道该实例类型的Foo时它非null ,因此第一颗子弹适用于这种情况; you just want to test for null . 您只想测试null This is the case in your updated question's example, the reference already has the compile-time type View . 在您更新的问题的示例中就是这种情况,引用已经具有编译时类型View

  • If you want to know whether a type cast (Foo)x will succeed, you may use x == null || x instanceof Foo 如果您想知道类型转换(Foo)x是否会成功,可以使用x == null || x instanceof Foo x == null || x instanceof Foo as the type cast will also succeed when x is null (however, you should think twice whether you really want to accept null s, even if the type cast will be successful) x == null || x instanceof Foo作为类型转换也将在xnull时成功执行(但是,即使类型转换成功,您也应该三思而行是否真的要接受null

Null check is not needed at all to check before instanceof . instanceof之前根本不需要空检查。 If it's null then during check of instanceof it will return false. 如果为null,则在instanceof检查期间将返回false。

Is a bad practice to test if something is null with instanceof ? 测试instanceof是否为空是一种不好的做法吗?

During runtime when you are not sure it can be null or not.just simply use instanceof it will return false in case of null object. 在运行时,如果您不确定它是否可以为null,则只需使用instanceof即可,如果为null,则返回false。

I just wonder if there is a good reason or practice of when to use != null instead of instanceof to check if something is null. 我只是想知道何时使用!= null而不是instanceof来检查某项是否为null是否有充分的理由或做法。

Of course there's a reason, the !=null can be applied to all objects in Java, but instanceof can't be applied to Primitive types it only checks if a given object is instance of a class, that's why we use !=null to check for nullity. 当然,有一个原因, !=null可以应用于Java中的所有对象,但是instanceof不能应用于Primitive类型,它仅检查给定对象是否是类的实例,这就是为什么我们将!=null应用于检查是否为空。

Is a bad practice to test if something is null with instanceof ? 测试instanceof是否为空是一种不好的做法吗?

You can use it, but why whould you use instanceof if it may cause problems with primitive types and if !=null can be applied to all objects. 您可以使用它,但是如果它可能导致原始类型出现问题并且如果!=null可以应用于所有对象,那么为什么要使用instanceof呢?

EDIT: 编辑:

In your case using if (view instanceof View) is much safer because you are using it to test if it's an instance of View and in the same time if it's not null . 在您的情况下,使用if (view instanceof View)更为安全,因为您正在使用它来测试它是否是View的实例,同时还要测试它是否不是null

And as you can see in " Here " you can see that you can use instanceof to check for nullity. 正如您在“ 此处 ”中看到的那样,您可以看到可以使用instanceof检查是否为空。

instanceof instruction is used to check if an object is an instance of a certain class. instanceof指令用于检查对象是否是某个类的实例。

So, using it to check null value simply makes no sense... 因此,使用它来检查空值根本没有任何意义...


In your code snippets you are checking two different things: 在代码段中,您正在检查两个不同的内容:

  • in the first one you are checking if your object is an instance of View class 在第一个中,您正在检查对象是否为View类的实例
  • in the second one you are checking if the object is initialized 在第二个中,您正在检查对象是否已初始化

So they are not two ways to do the same thing... 所以他们不是做同一件事的两种方式...

Take a look: use of "Instance of" in java 看看: 在Java中使用“实例”

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

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