Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java instanceof

    Java's instanceof operator is used to determine whether a given object is an instance of the class, subclass, or interface that is specified.

    Because it compares the instance to the type, Java's instanceof operator is also known as the type comparison operator. It returns either true or false. When we use the instanceof operator on a variable that has a null value, it returns false.

Table Of Content
  • What is instanceof in Java
  • Simple example of java instanceof
  • Downcasting with java instanceof operator
  • instanceof returning false for null
  • Real use of instanceof operator






Simple example of java instanceof


simple example of instance operator
 // Java example instanceof operator 
class Main{
	public static void main(String args[]){  
		Main main=new Main(); 
		System.out.println( main instanceof Main);
	}  
}




Output:

true 

Another example of java instanceof operator


Java example of java instanceof operator Type
 // Java example of instanceof operator 
class Vehicels{}
    class Car extends Vehicels{
	public static void main(String args[]){  
		Car car=new Car(); 
		System.out.println( car instanceof Vehicels);
	}  
}



Output:

true 

Downcasting with java instanceof operator


Downcasting occurs when the Subclass type refers to the object of the Parent class. If we do it directly, the compiler returns a Compilation error. If you typecast it, a ClassCastException is thrown at runtime. Downcasting is possible, however, if we use the instanceof operator.


Car car=new Vehicles(); //C.T error  


When we typecast downcasting, a ClassCastException is thrown at runtime.


Car car=(Car)new Vehicles();//It will throw ClassCastException exception at run time  





Java example of java instanceof operator Type
 // Java example of instanceof operator 
class Vehicels{}
    class Car extends Vehicels{
  static void method(Vehicels vehicels) {  
	   if(vehicels instanceof Car){  
	     Car car=(Car)vehicels; //downcasting performed
	     System.out.println("Downcasting");  
	   }  
  } 
	public static void main(String args[]){  
		Vehicels vehicels=new Car(); 
		Car.method(vehicels);
	}  
}



Output:

Downcasting 


instanceof returning false for null


Example of java instanceof operator returning false for null
 // Example of instanceof operator 
class Vehicles {}

class Car extends Vehicles {}

class Main {
	public static void main(String[] args)
	{
		Car car = null;

		if (car instanceof Car)
			System.out.println("car is an instance of Car Class");
		else
			System.out.println("car is not an instance of Car Class");

		if (car instanceof Vehicles)
			System.out.println("car is an instance of Vehicles Class");
		else
			System.out.println("car is not an instance of the Vehicles Class.");
		
		if (car instanceof Object)
			System.out.println("car is an instance of Object");
		else
			System.out.println("car is not instance of Object");
	}
}



Output:

car is not an instance of Car Class 
car is not an instance of Vehicles Class 
car is not an instance of Object 


Real use of instanceof operator


Example of java instanceof operator
 // Example of instanceof operator 
class Vehicles {}

class Car extends Vehicles {}

class Main {
	public static void main(String[] args)
	{
		Car car = new Car();

		if (car instanceof Car)
			System.out.println("car is an instance of Car Class");
		else
			System.out.println("car is not an instance of Car Class");

		if (car instanceof Vehicles)
			System.out.println("car is an instance of Vehicles Class");
		else
			System.out.println("car is not an instance of the Vehicles Class.");
		
		if (car instanceof Object)
			System.out.println("car is an instance of Object");
		else
			System.out.println("car is not instance of Object");
	}
}



Output:

car is an instance of Car Class 
car is an instance of Vehicles Class 
car is an instance of Object 
Java Object Cloning Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.