instanceof Vs getClass() [英] instanceof Vs getClass( )

查看:127
本文介绍了instanceof Vs getClass()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 getClass() == 运算符在 instanceOf 运算符。

  Object str = new Integer(2000); 

long starttime = System.nanoTime();

if(str instanceof String){
System.out.println(its string);
} else {
if(str instanceof Integer){
System.out.println(its integer);

}
}

System.out.println((System.nanoTime() - starttime));

starttime = System.nanoTime();

if(str.getClass()== String.class){
System.out.println(its string in equals);
} else {
if(str.getClass()== Integer.class){
System.out.println(which integer);
}
}

System.out.println((System.nanoTime() - starttime));

是否有任何指南,使用 getClass c $ c>或 instanceOf



给定一个场景:我知道要匹配的精确类, code> String Integer (这些是最终类)等。



使用 instanceOf 运算符不良做法?

解决方案

instanceof getClass()== ... 的性能不同是他们做不同的事情。 / p>


  • instanceof 测试左侧的对象引用LHS)是右侧类型(RHS)或某些子类型的实例。


  • getClass()== ... 测试类型是否相同。




因此,建议忽略性能问题,并使用替代方法为您提供所需的答案。



是的,过度使用它们都是design smell 。如果你不小心,你最终得到一个设计,其中添加新的子类导致大量的代码重做。在大多数情况下,首选方法是使用多态性。



(有例外。一个典型的例子是在执行 equals(Object)你需要测试参数的类型,如果不匹配,返回 false 在这种特殊情况下,你通常需要使用 getClass()在子类面前正确地实现 equals 的合同。)


I see gain in performance when using getClass() and == operator over instanceOf operator.

Object  str = new Integer("2000");

long starttime = System.nanoTime();

if(str instanceof String) {
    System.out.println("its string");
} else {
    if (str instanceof Integer) {
        System.out.println("its integer");

    }
}

System.out.println((System.nanoTime()-starttime));

starttime = System.nanoTime();

if(str.getClass() == String.class) {
    System.out.println("its string in equals");
} else {
    if(str.getClass() == Integer.class) {
        System.out.println("its integer");
    }
}

System.out.println((System.nanoTime()-starttime));

Is there any guideline, which one to use getClass() or instanceOf?

Given a scenario: I know exact classes to be matched, that is String, Integer (these are final classes), etc.

Is using instanceOf operator bad practise ?

解决方案

The reason that the performance of instanceof and getClass() == ... is different is that they are doing different things.

  • instanceof tests whether the object reference on the left-hand side (LHS) is an instance of the type on the right-hand side (RHS) or some subtype.

  • getClass() == ... tests whether the types are identical.

So the recommendation is to ignore the performance issue and use the alternative that gives you the answer that you need.

And yes, overuse of either of them is "design smell". If you are not careful, you end up with a design where the addition of new subclasses results in a significant amount of code reworking. In most situations, the preferred approach is to use polymorphism.

(There are exceptions. A classic one is in the implementation of equals(Object), in which you need to test the type of the argument, and return false if it doesn't match. In this particular case, you usually need to use getClass() to implement the contract of equals correctly in the face of subclasses.)

这篇关于instanceof Vs getClass()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆