← Back to Core Java Course | Chapter 7: OOP Core | Lesson 10 of 11

Java का instanceof Operator

instanceof पूछता है कि एक object एक certain type है या नहीं, यह जांचने जैसा कि एक animal एक dog है या नहीं। यह true या false answer देता है।
Syntax
java
if (object instanceof ClassName) {
    // object is a ClassName
}
if (object instanceof ClassName name) {
    // pattern matching: name is already cast
}

What is the instanceof Operator?

instanceof runtime पर test करता है कि एक object एक दी class या interface का instance है या नहीं, एक plain boolean return करते हुए — एक type-specific operation perform करने से पहले किसी object का असली type जांचने का standard तरीका, खासकर polymorphic collections के साथ।

उदाहरण: What is the instanceof Operator?

java
// Define the class Animal
class Animal {}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Animal object and store it in a
		Animal a = new Animal();
		// Print a line to the console
		System.out.println(a instanceof Animal);
	}
}

Inheritance Checks

चूंकि subclasses को उनके parent classes के instances भी माना जाता है, dog instanceof Animal true return करता है अगर Dog extends Animal, भले ही check object के exact concrete class के बजाय parent type का नाम ले।

उदाहरण: Inheritance Checks

java
class Animal {}
class Dog extends Animal {}
public class Main {
	public static void main(String[] args) {
		Dog dog = new Dog();
		System.out.println(dog instanceof Animal); // true: Dog extends Animal
	}
}

Checking Null References

एक null reference कभी किसी चीज़ का instance नहीं है, इसलिए एक null variable पर instanceof हमेशा false evaluate होता है चाहे आप किस class के खिलाफ check कर रहे हों — यह instanceof को बिना पहले NullPointerException का जोखिम उठाए किसी variable का type जांचने का एक safe तरीका बनाता है।

उदाहरण: Checking Null References

java
class Animal {}
public class Main {
	public static void main(String[] args) {
		Animal a = null;
		System.out.println(a instanceof Animal); // always false for null
	}
}

Object Class Checks

चूंकि Java में हर class implicitly Object extend करती है, किसी भी non-null object instance को instanceof से Object के खिलाफ check करना हमेशा true return करेगा — यह अकेले शायद ही कभी उपयोगी है लेकिन यह illustrate करता है कि class hierarchy असल में कितनी गहरी जाती है।

उदाहरण: Object Class Checks

java
class Animal {}
public class Main {
	public static void main(String[] args) {
		Animal a = new Animal();
		System.out.println(a instanceof Object); // true: every class extends Object
	}
}

Interface Checks

instanceof classes तक सीमित नहीं — आप यह भी जांच सकते हैं कि किसी object की class एक particular interface implement करती है या नहीं, जो आम है जब आपको confirm करना हो कि एक object एक capability support करता है (जैसे Comparable) इसे चाहने वाले methods call करने से पहले।

उदाहरण: Interface Checks

java
// Define the interface Flyable
interface Flyable {}
// Define the class Bird
class Bird implements Flyable {}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Bird object and store it in bird
		Bird bird = new Bird();
		// Print a line to the console
		System.out.println(bird instanceof Flyable);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. null पर instanceof call करना, जो false return करता है, इसलिए check एक missing object छुपा सकता है।
  2. Unrelated classes के साथ instanceof उपयोग करना, जो एक compile error है।
  3. एक downcast से पहले instanceof check skip करना, इसलिए एक ClassCastException throw होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.