Java instanceof Operator Example
The instanceof operator also known as type comparison operator, and it's very useful to compare the type of object. It compares an object to a specified type. One can use instanceof operator to test if an object is an instance of class,sub-class or interface, just like checking of serializable interface after implementation of serializable. In this post, I am going to show you few examples of how to use instanceof operator in your code. Example 1: We know that, String class by default implement the Serializable interface,lets check this. package com.javamakeuse.poc.bd; import java.io.Serializable; public class InstanceOfOp { public static void main(String[] args) { String str = "implemented serializable"; if (str instanceof Serializable) { System.out.println("String by default implement Serializable"); } } } Output: String by default implement Serializable Example 2: package com.javamakeuse.poc.bd; public class InstanceOfOp { public s...