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("its integer");
}
}
System.out.println((System.nanoTime()-starttime));
是否有任何指南,使用哪一个或?getClass()
instanceOf
给定一个场景:我知道要匹配的确切类,即,(这些是最终类)等。String
Integer
使用操作员是否不好?instanceOf