Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Explain about instanceof operator in java

An instanceof in Java is a comparison operator which, given an object instance, 
checks whether that instance is of a specified type (class/sub-class/interface) 
or not. Just like other comparison operators, it returns true or false.

Comparing any object instance with a null type through instanceof operator 
returns a false.
  
Instanceof operator is used to test the object is of which type.

Syntax : <reference expression> instanceof <destination type>
Instanceof returns true if reference expression is subtype of destination type.
Instanceof returns false if reference expression is null.
Comment

Java instanceof Keyword

public class Main {
  public static void main(String[] args) {
    Main myObj = new Main();
    System.out.println(myObj instanceof Main); // returns true
  }
}
Comment

Java instanceof

class Main {

  public static void main(String[] args) {

    // create a variable of string type
    String name = "Programiz";
    
    // checks if name is instance of String
    boolean result1 = name instanceof String;
    System.out.println("name is an instance of String: " + result1);

    // create an object of Main
    Main obj = new Main();

    // checks if obj is an instance of Main
    boolean result2 = obj instanceof Main;
    System.out.println("obj is an instance of Main: " + result2);
  }
}
Comment

java instanceof example

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

Java instanceof Operator

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

    String str = "Programiz";
    boolean result;

    // checks if str is an instance of
    // the String class
    result = str instanceof String;
    System.out.println("Is str an object of String? " + result);
  }
}
Comment

Java instanceof

// Java Program to check if an object of the subclass
// is also an instance of the superclass
// superclass
class Animal {
}

// subclass
class Dog extends Animal {
}
class Main {
  public static void main(String[] args) {
    // create an object of the subclass
    Dog d1 = new Dog();
    // checks if d1 is an instance of the subclass
    System.out.println(d1 instanceof Dog);        // prints true
    // checks if d1 is an instance of the superclass
    System.out.println(d1 instanceof Animal);     // prints true
  }
}
Comment

instanceof java

@Test
public void givenWhenInstanceIsCorrect_thenReturnTrue() {
    Ring ring = new Ring();
    Assert.assertTrue(ring instanceof Round);
}
Comment

instanceof Operator Java

objectName instanceOf className;
Comment

PREVIOUS NEXT
Code Example
Java :: fibonacci of 6 
Java :: arrays methods in java 
Java :: is c# similar to java 
Java :: what is a float java 
Java :: while loops java 
Java :: Spigot how to get block player is looking at 
Java :: radiogroup get selected item android 
Java :: android bottomnav fab 
Java :: Java empty() Method 
Java :: What would be the behavior if this() and super() used in a method? 
Java :: java static keyword 
Java :: arrays with for loops 
Java :: java multi thread 
Java :: JAVA Multi-line Comment 
Java :: java array quick sort 
Java :: Java Create a Scanner Object in Java 
Java :: data structures in java 
Java :: java creating an arraylist 
Java :: declare matrix in java 
Java :: default constructor 
Java :: loop and save letters in a string java 
Java :: top java interview coding questions 
Java :: list of arrays 
Java :: java string class 
Java :: combobox index out of bound javafx 
Java :: how to install volley java 
Java :: java disable stack trace 
Java :: rotate vector 3d java 
Java :: find the key that has the least value in map java 
Java :: trier un tableau de string java 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =