BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java instanceof With Examples

By Chaitanya Singh | Filed Under: java

The instanceof keyword is an operator in java. It checks if the given object is an instance of a specified class or interface. It returns true or false.

Let’s take a simple example first, to see how instanceof works:

public class JavaExample {
  public static void main(String[] args) {
    //the obj is an object of "JavaExample" class
    JavaExample obj = new JavaExample();
    System.out.println(obj instanceof JavaExample);
  }
}

Output:

true

It returned true because obj is an object of the specified class.

How instanceof works?

  • It compares the left operand (the object) with the right operand (the type specified by class name or interface name) and returns true, if the type matches else it returns false.
  • When I say type of class, it means that if you compare the object of sub class with the super class using instanceof, it will still return true.

Example 1: Using instanceof with strings

public class JavaExample {
  public static void main(String[] args) {
    String str = "Java is so cool";
    System.out.println(str instanceof java.lang.String);
  }
}

Output:

true

Example 2: Using instanceof with null

If an object contains null, then it will always return false.

public class JavaExample {
  public static void main(String[] args) {
    JavaExample obj = null;
    System.out.println(obj instanceof JavaExample);
  }
}

Output:

false

Example 3: subclass and superclass

Here, we have a superclass and a subclass. When we compared the object of subclass with the superclass, the instanceof operator returned true.

class MyClass{
}
public class JavaExample extends MyClass{
  public static void main(String[] args) {
    JavaExample obj = new JavaExample();
    //checking subclass object for superclass type
    System.out.println(obj instanceof MyClass);
  }
}

Output:

true

Example 4: instanceof with interface

interface MyInterface{
}
public class JavaExample implements MyInterface{
  public static void main(String[] args) {
    JavaExample obj = new JavaExample();
    //comparing subclass object with parent interface
    System.out.println(obj instanceof MyInterface);
  }
}

Output:

true

Recommended Posts

  • Java Super Keyword
  • Java OOPs Concepts Tutorial
  • Java Access Modifiers Tutorial
  • Java Method Overriding Tutorial
  • Java Interface Tutorial
❮ Java Keywords

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap