instanceof

Why can't a “Class” variable be passed to instanceof?

此生再无相见时 提交于 2019-11-26 19:26:32
问题 Why doesn't this code compile? public boolean isOf(Class clazz, Object obj){ if(obj instanceof clazz){ return true; }else{ return false; } } Why I can't pass a class variable to instanceof ? 回答1: The instanceof operator works on reference types, like Integer , and not on objects, like new Integer(213) . You probably want something like clazz.isInstance(obj) Side note: your code will be more concise if you write public boolean isOf(Class clazz, Object obj){ return clazz.isInstance(obj) } Not

instanceof Vs getClass( )

瘦欲@ 提交于 2019-11-26 18:46:08
问题 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() ==

java optimization nitpick: is it faster to cast something and let it throw exception than calling instanceof to check before cast?

二次信任 提交于 2019-11-26 18:28:10
问题 Before anyone says anything I'm asking this out of curiosity only; I'm not planning to do any premature optimization based off of this answer. My question is about speed in using reflection and casting. The standard saying is 'reflection is slow'. My question is what part exactly is slow, and why; particularly in comparing if something is a parent of another instance. I'm pretty confident that just comparing the class of an object to another Class object is about as fast as any comparison,

小白之旅10-1

怎甘沉沦 提交于 2019-11-26 17:36:39
一. 多态 1.1 概念: 多态是继封装、继承后,面向对象的第三大特性。 例如:Student类继承了Human类,那么这个Student对象既是一个Student同时也是一个Human。 1.2 多态的体现 代码中的格式: 父类引用变量指向子类对象,Student对象可以赋值给Student也可以赋值给Human 父类类型 变量名 = new 子类类型(); Human s = new Student(); 多态的前提:必须在继承(实现)关系中 1.3 多态中成员变量的特点 当子父类中出现同名成员变量时, 编译时期:要参考父类中是否有该成员变量,如果没有则编译失败 运行时期:调用的是父类中的成员变量 简而言之 :编译运行都看左边 1.4 多态中成员方法的特点 当子父类中出现同名成员方式时, 编译时期:要参考父类中是否有该成员方法,如果没有则编译失败 运行时期:调用的是子类重写后的方法 简而言之 :编译看左边,运行看右边 1.5 多态的转型 多态的转型分为向上转型和向下转型 1、向上转型: 当子类对象赋值给父类引用变量时,就是向上转型,多态本身就是向上转型的过程。 格式: 父类类型 变量名 = new 子类类型(); 2、向下转型: 一个已经向上转型的子类对象可以使用强制类型转换,将父类的引用变量转为子类引用,这个过程就是向下转型。直接创建子类或者父类对象是不能向下转型的。 注

How to avoid 'instanceof' when implementing factory design pattern?

风流意气都作罢 提交于 2019-11-26 17:25:01
I am attempting to implement my first Factory Design Pattern, and I'm not sure how to avoid using instanceof when adding the factory-made objects to lists. This is what I'm trying to do: for (ABluePrint bp : bluePrints) { AVehicle v = AVehicleFactory.buildVehicle(bp); allVehicles.add(v); // Can I accomplish this without using 'instanceof'? if (v instanceof ACar) { cars.add((ACar) v); } else if (v instanceof ABoat) { boats.add((ABoat) v); } else if (v instanceof APlane) { planes.add((APlane) v); } } From what I've read on SO, using 'instanceof' is a code smell. Is there a better way to check

Java: Instanceof and Generics

痴心易碎 提交于 2019-11-26 17:16:44
Before I look through my generic data structure for a value's index, I'd like to see if it is even an instance of the type this has been parametrized to. But Eclipse complains when I do this: @Override public int indexOf(Object arg0) { if (!(arg0 instanceof E)) { return -1; } This is the error message: Cannot perform instanceof check against type parameter E. Use instead its erasure Object since generic type information will be erased at runtime What is the better way to do it? The error message says it all. At runtime, the type is gone, there is no way to check for it. You could catch it by

How does one use polymorphism instead of instanceof? (And why?)

牧云@^-^@ 提交于 2019-11-26 16:52:54
问题 If we take the code below: Shape p1 = new Square(); Square c1; if(p1 instanceof Square) { c1 = (Square) p1; } What does it mean to prefer polymorphism to instanceof , and incidentally, why is it better? Edit: I understand what polymorphism is; what I'm missing is how one would use it rather than instanceof . 回答1: The main difference between if...else... (or switch, or Visitor), and between polymorphism is modularity. There's so called open-closed principle, which basically means, that when

Why cast after an instanceOf?

只谈情不闲聊 提交于 2019-11-26 16:51:52
问题 In the example below (from my coursepack), we want to give to the Square instance c1 the reference of some other object p1 , but only if those 2 are of compatible types. if (p1 instanceof Square) {c1 = (Square) p1;} What I don't understand here is that we first check that p1 is indeed a Square , and then we still cast it. If it's a Square , why cast? I suspect the answer lies in the distinction between apparent and actual types, but I'm confused nonetheless... Edit: How would the compiler

Check if a object is a instance of a class (but not a instance of its subclass)

落爺英雄遲暮 提交于 2019-11-26 16:21:06
问题 For this example: public class Foo{} public class Bar extends Foo{} .... void myMethod(Foo qux){ if (checkInstance(qux,Foo.class)){ .... } } How can I check if qux is a instance of Foo (but not a instance of its subclass of foo)? That is: checkInstance(qux,Foo.class)=true checkInstance(qux,Bar.class)=false Is there some kind of statement like instanceof for this check? or I should use qux.getClass().equals(Foo.class) 回答1: If you have to do this, the only way would be the getClass().equals(Foo

Is instanceof considered bad practice? If so, under what circumstances is instanceof still preferable?

六眼飞鱼酱① 提交于 2019-11-26 16:07:29
Over the years, I've tried to avoid instanceof whenever possible. Using polymorphism or the visitor pattern where applicable. I suppose it simply eases maintenance in some situations... Are there any other drawbacks that one should be aware of? I do however see it here and there in the Java libraries so I suppose it has its place? Under what circumstances is it preferable? Is it ever unavoidable? I can imagine some cases, for example you have some objects of a library, which you can't extend (or it would be inconvenient to do so), perhaps mixed with some objects of your, all with same base