In Java, can I use a primitive type literal or type variable in an instanceof expression?

Nope, because of type erasure An instance of MyClass { private Class clazz; MyClass(Class clazz) { this. Clazz = clazz; } // Now you can use clazz to check for instances, create new instances ect. } For the second one, the problem is the first operand, not the second.

The primitive value itself isn't an instance of Integer the boxed version is: Object obj = 2; boolean b2 = obj instanceof Integer Whenever you've got a genuine primitive value, you'll already know the type so making a dynamic type check doesn't make much sense.

Nope, because of type erasure. An instance of MyClass doesn't actually know what T is. You need to have an instance of Class.

Then you can use the isInstance method. One way of doing that is to specify it in the constructor: class MyClass { private Class clazz; MyClass(Class clazz) { this. Clazz = clazz; } // Now you can use clazz to check for instances, create new instances ect.

} For the second one, the problem is the first operand, not the second. The primitive value itself isn't an instance of Integer; the boxed version is: Object obj = 2; boolean b2 = obj instanceof Integer; Whenever you've got a genuine primitive value, you'll already know the type so making a dynamic type check doesn't make much sense.

Due to type erasure, you cannot know what T is. Literals (except for string literals) aren't objects. Therefore, no.

Basically, instanceof askes for an object as left operand. Primitive variables are not objects, so no, you can't use it that way.

You can't do it. Even if you could, you can't use it. A typical usage of instanceof looks like void somemethod(Collection c) { if (c instanceof List) {...} } somemethod(new ArrayList()); The important thing here is that you get an object of a supertype (here: Collection) which may or may not be instance of a subtype (here: List).

With primitives this is impossible: void anothermethod(double x) { ....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions