Linked Questions
39 questions linked to/from What is the difference between instanceof and Class.isAssignableFrom(...)?
2 votes
1 answer
729 views
Check if class implements interface given the class name [duplicate]
Given String className = "com.example.MyClass"; How would I be able to check if this class implements a specific interface? I've tried Class myClass = Class.forName(className); if (myClass ...
0 votes
1 answer
67 views
Java: instanceof doesn't compile [duplicate]
I'm a newbie to Java and want to write a method checking if this object is of a sub class of given class <p_SuperClassName>. Eclipse denies compilation of the following code giving the message ...
362 votes
24 answers
151k views
The performance impact of using instanceof in Java
I am working on an application and one design approach involves extremely heavy use of the instanceof operator. While I know that OO design generally tries to avoid using instanceof, that is a ...
320 votes
4 answers
952k views
Use of "instanceof" in Java [duplicate]
What is the 'instanceof' operator used for? I learned that Java has the instanceof operator. Can you elaborate where it is used and what are its advantages?
266 votes
4 answers
179k views
java: Class.isInstance vs Class.isAssignableFrom
Let clazz be some Class and obj be some Object. Is clazz.isAssignableFrom(obj.getClass()) always the same as clazz.isInstance(obj) ? If not, what are the differences?
114 votes
5 answers
89k views
Determine if a Class implements a interface in Java
I have a Class object. I want to determine if the type that the Class object represents implements a specific interface. I was wondering how this could be achieved? I have the following code. ...
51 votes
8 answers
72k views
Java Generic Class - Determine Type
If I am creating a java class to be generic, such as: public class Foo<T> How can one determine internally to that class, what 'T' ended up being? public ???? Bar() { //if its type 1 ...
30 votes
9 answers
15k views
Equivalent of the C# keyword 'as' in Java
In Java, is it possible to attempt a cast and get back null if the cast fails?
24 votes
3 answers
19k views
Java Factory Pattern With Generics
I would like my BallUserInterfaceFactory to return an instance of a user interface that has the proper generic type. I am stuck in the example below getting the error: Bound mismatch: The generic ...
14 votes
5 answers
17k views
Java instanceof with class name
I am just curious to ask this, maybe it is quite meaningless. When we are using instanceof in java, like: if (a instanceof Parent){ //"Parent" here is a parent class of "a" } why we can't use like ...
8 votes
4 answers
12k views
How do I use getClass() to tell if an Object is numeric?
I want to dynamically tell if an Object in Java is numeric or not. My code is as the following: if(obj.getClass().equals(Number.class)) { attributeTypeStr = "Numeric"; } else { ...
6 votes
5 answers
8k views
Does instanceof operator generate a lot of overhead ? Why? [duplicate]
I have a colleague here in my project, who is deeply against the use of instanceof operator, because it "generates a lot of overhead", what is the reason for that? Is it true? Is there another way to ...
3 votes
7 answers
14k views
While Not Condition
How can I say the following: while(input is not an int){ do this } I tried this code but I know it's wrong: int identificationnumber; Scanner sc3 = new Scanner(System.in); identificationnumber = ...
3 votes
5 answers
5k views
Compare object type
I don't know how to perform type checking on newEntry, I want to make sure that it is of type MyTable (without creating an object of MyTable). public static boolean add(String table, Object newEntry) ...
3 votes
2 answers
33k views
How to write a test case for a method returning object
I have a method for which the return type is object. How do I create a test case for this? How do I mention that the result should be an object? e.g.: public Expression getFilter(String expo) { /...