开发者

What is the 'instanceof' operator used for in Java?

开发者 https://www.devze.com 2023-04-02 09:03 出处:网络
What is the instanceof operator 开发者_运维知识库used for? I\'ve seen stuff like if (source instanceof Button) {

What is the instanceof operator 开发者_运维知识库used for? I've seen stuff like

if (source instanceof Button) {
    //...
} else {
    //...
}

But none of it made sense to me. I've done my research, but came up only with examples without any explanations.


instanceof keyword is a binary operator used to test if an object (instance) is a subtype of a given Type.

Imagine:

interface Domestic {}
class Animal {}
class Dog extends Animal implements Domestic {}
class Cat extends Animal implements Domestic {}

Imagine a dog object, created with Object dog = new Dog(), then:

dog instanceof Domestic // true - Dog implements Domestic
dog instanceof Animal   // true - Dog extends Animal
dog instanceof Dog      // true - Dog is Dog
dog instanceof Object   // true - Object is the parent type of all objects

However, with Object animal = new Animal();,

animal instanceof Dog // false

because Animal is a supertype of Dog and possibly less "refined".

And,

dog instanceof Cat // does not even compile!

This is because Dog is neither a subtype nor a supertype of Cat, and it also does not implement it.

Note that the variable used for dog above is of type Object. This is to show instanceof is a runtime operation and brings us to a/the use case: to react differently based upon an objects type at runtime.

Things to note: expressionThatIsNull instanceof T is false for all Types T.


It's an operator that returns true if the left side of the expression is an instance of the class name on the right side.

Think about it this way. Say all the houses on your block were built from the same blueprints. Ten houses (objects), one set of blueprints (class definition).

instanceof is a useful tool when you've got a collection of objects and you're not sure what they are. Let's say you've got a collection of controls on a form. You want to read the checked state of whatever checkboxes are there, but you can't ask a plain old object for its checked state. Instead, you'd see if each object is a checkbox, and if it is, cast it to a checkbox and check its properties.

if (obj instanceof Checkbox)
{
    Checkbox cb = (Checkbox)obj;
    boolean state = cb.getState();
}


As described on this site:

The instanceof operator can be used to test if an object is of a specific type...

if (objectReference instanceof type)

A quick example:

String s = "Hello World!"
return s instanceof String;
//result --> true

However, applying instanceof on a null reference variable/expression returns false.

String s = null;
return s instanceof String;
//result --> false

Since a subclass is a 'type' of its superclass, you can use the instanceof to verify this...

class Parent {
    public Parent() {}
}

class Child extends Parent {
    public Child() {
        super();
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        System.out.println( child instanceof Parent );
    }
}
//result --> true

I hope this helps!


This operator allows you to determine the type of an object. It returns a boolean value.

For example

package test;

import java.util.Date;
import java.util.Map;
import java.util.HashMap;

public class instanceoftest
{
    public static void main(String args[])
    {
        Map m=new HashMap();
        System.out.println("Returns a boolean value "+(m instanceof Map));
        System.out.println("Returns a boolean value "+(m instanceof HashMap));
        System.out.println("Returns a boolean value "+(m instanceof Object));
        System.out.println("Returns a boolean value "+(m instanceof Date));
    }
} 

the output is:

Returns a boolean value true
Returns a boolean value true
Returns a boolean value true
Returns a boolean value false


If source is an object variable, instanceof is a way of checking to see if it is a Button or not.


As mentioned in other answers, the canonical typical usage of instanceof is for checking if an identifier is referring to a more specific type. Example:

Object someobject = ... some code which gets something that might be a button ...
if (someobject instanceof Button) {
    // then if someobject is in fact a button this block gets executed
} else {
    // otherwise execute this block
}

Note however, that the type of the left-hand expression must be a parent type of the right hand expression (see JLS 15.20.2 and Java Puzzlers, #50, pp114). For example, the following will fail to compile:

public class Test {
    public static void main(String [] args) {
        System.out.println(new Test() instanceof String); // will fail to compile
    }
}

This fails to compile with the message:

Test.java:6: error: inconvertible types
        System.out.println(t instanceof String);
                       ^
  required: String
  found:    Test
1 error

As Test is not a parent class of String. OTOH, this compiles perfectly and prints false as expected:

public class Test {
    public static void main(String [] args) {
        Object t = new Test();
        // compiles fine since Object is a parent class to String
        System.out.println(t instanceof String); 
    }
}


public class Animal{ float age; }

public class Lion extends Animal { int claws;}

public class Jungle {
    public static void main(String args[]) {

        Animal animal = new Animal(); 
        Animal animal2 = new Lion(); 
        Lion lion = new Lion(); 
        Animal animal3 = new Animal(); 
        Lion lion2 = new Animal();   //won't compile (can't reference super class object with sub class reference variable) 

        if(animal instanceof Lion)  //false

        if(animal2 instanceof Lion)  //true

        if(lion insanceof Lion) //true

        if(animal3 instanceof Animal) //true 

    }
}


Most people have correctly explained the "What" of this question but no one explained "How" correctly.

So here's a simple illustration:

String s = new String("Hello");
if (s instanceof String) System.out.println("s is instance of String"); // True
if (s instanceof Object) System.out.println("s is instance of Object"); // True
//if (s instanceof StringBuffer) System.out.println("s is instance of StringBuffer"); // Compile error
Object o = (Object)s;
if (o instanceof StringBuffer) System.out.println("o is instance of StringBuffer"); //No error, returns False
else System.out.println("Not an instance of StringBuffer"); // 
if (o instanceof String) System.out.println("o is instance of String"); //True

Outputs:

s is instance of String
s is instance of Object
Not an instance of StringBuffer
o is instance of String

The reason for compiler error when comparing s with StringBuffer is well explained in docs:

You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

which implies the LHS must either be an instance of RHS or of a Class that either implements RHS or extends RHS.

How to use use instanceof then?
Since every Class extends Object, type-casting LHS to object will always work in your favour:

String s = new String("Hello");
if ((Object)s instanceof StringBuffer) System.out.println("Instance of StringBuffer"); //No compiler error now :)
else System.out.println("Not an instance of StringBuffer");

Outputs:

Not an instance of StringBuffer


Can be used as a shorthand in equality check.

So this code

if(ob != null && this.getClass() == ob.getClass) {
}

can be written as

if(ob instanceOf ClassA) {
}
 


The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

http://download.oracle.com/javase/tutorial/java/nutsandbolts/op2.html


Instance of keyword is helpful when you want to know particular object's instance .

Suppose you are throw exception and when you have catch then perform sum custom operation and then again continue as per your logic (throws or log etc)

Example : 1) User created custom exception "InvalidExtensionsException" and throw it as per logic

2) Now in catch block catch (Exception e) { perform sum logic if exception type is "InvalidExtensionsException"

InvalidExtensionsException InvalidException =(InvalidExtensionsException)e;

3) If you are not checking instance of and exception type is Null pointer exception your code will break.

So your logic should be inside of instance of if (e instanceof InvalidExtensionsException){ InvalidExtensionsException InvalidException =(InvalidExtensionsException)e; }

Above example is wrong coding practice However this example is help you to understand use of instance of it.


Best explanation is jls. Always try to check what source says. There you will get the best answer plus much more. Reproducing some parts here:

The type of the RelationalExpression operand of the instanceof operator must be a reference type or the null type; otherwise, a compile-time error occurs.

It is a compile-time error if the ReferenceType mentioned after the instanceof operator does not denote a reference type that is reifiable (§4.7).

If a cast (§15.16) of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.


The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).

The instanceof in java is also known as type comparison operator as it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has null value, it returns false.

From JDK 14+ which includes JEP 305 we can also do "Pattern Matching" for instanceof

Patterns basically test that a value has a certain type, and can extract information from the value when it has the matching type. Pattern matching allows a more clear and efficient expression of common logic in a system, namely the conditional removal of components from objects.

Before Java 14

if (obj instanceof String) {
    String str = (String) obj; // need to declare and cast again the object
    .. str.contains(..) ..
}else{
     str = ....
}

Java 14 enhancements

if (!(obj instanceof String str)) {
    .. str.contains(..) .. // no need to declare str object again with casting
} else {
    .. str....
}

We can also combine the type check and other conditions together

if (obj instanceof String str && str.length() > 4) {.. str.contains(..) ..}

The use of pattern matching in instanceof should reduce the overall number of explicit casts in Java programs.

PS: instanceOf will only match when the object is not null, then only it can be assigned to str.


instanceof operator can also be used when the type of the elements present in the object(listObj) is unknown at run time. In those cases instanceof operator can be used to figure out the elements type and will be helpful to proceed further based on the requirement.

For Example :

   String str = "";
   int a = 0;
   Integer b = null;

    List listObj = new ArrayList<>();
    listObj.add("String");
    listObj.add(100);
    listObj.add(10.5);
    listObj.add(1l);
    
    if (listObj.get(0) instanceof String) {
        System.out.println("String");
        str = (String)listObj.get(0);
    }
    
    if (listObj.get(1) instanceof Integer) {
        System.out.println("Integer");
         a = (int)listObj.get(1);
         b = (Integer)listObj.get(1);
    }
    
    if (listObj.get(2) instanceof Double) {
        System.out.println("Double");
    }
    
    if (listObj.get(3) instanceof Long) {
        System.out.println("Long");
    }

if the value retrieved from the object is assigned to a variable,JVM will ask you to cast it to a particular type at compile time.

lets consider :

int x = (String)listObj.get(0); 

// In above example , element retrieved from listObj is String and is casted to int. This will resolve the compile time errors.But at the time of execution JVM will throw ClassCastException.

so instead of randomly assigning values to a variable that does not matches the type, we can use instanceof operator to check and assign the values to the correct variables to avoid errors.


class Test48{
public static void main (String args[]){
Object Obj=new Hello();
//Hello obj=new Hello;
System.out.println(Obj instanceof String);
System.out.println(Obj instanceof Hello);
System.out.println(Obj instanceof Object);
Hello h=null;
System.out.println(h instanceof Hello);
System.out.println(h instanceof Object);
}
}  


Very simple code example:

If (object1 instanceof Class1) {
   // do something
} else if (object1 instanceof Class2) {
   // do something different
}

Be careful here. In the example above, if Class1 is Object, the first comparison will always be true. So, just like with exceptions, hierarchical order matters!


You could use Map to make higher abstraction on instance of

private final Map<Class, Consumer<String>> actions = new HashMap<>();

Then having such map add some action to it:

actions.put(String.class, new Consumer<String>() {
        @Override
        public void accept(String s) {
           System.out.println("action for String");       
        }
    };

Then having an Object of not known type you could get specific action from that map:

actions.get(someObject).accept(someObject)


The instanceof operator is used to check whether the object is an instance of the specified type. (class or subclass or interface).

The instanceof is also known as type comparison operator because it compares the instance with type. It returns either true or false.

class Simple1 {  
public static void main(String args[]) {  
Simple1 s=new Simple1();  
System.out.println(s instanceof Simple1); //true  
}  
}  

If we apply the instanceof operator with any variable that has null value, it returns false.

0

精彩评论

暂无评论...
验证码 换一张
取 消