Java का instanceof Operator
In this page:
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?
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Inheritance Checks
चूंकि subclasses को उनके parent classes के instances भी माना जाता है, dog instanceof Animal true return करता है अगर Dog extends Animal, भले ही check object के exact concrete class के बजाय parent type का नाम ले।
उदाहरण: Inheritance Checks
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
}
}
Login to try C/C++/Java/PHP code in the editor
Checking Null References
एक null reference कभी किसी चीज़ का instance नहीं है, इसलिए एक null variable पर instanceof हमेशा false evaluate होता है चाहे आप किस class के खिलाफ check कर रहे हों — यह instanceof को बिना पहले NullPointerException का जोखिम उठाए किसी variable का type जांचने का एक safe तरीका बनाता है।
उदाहरण: Checking Null References
class Animal {}
public class Main {
public static void main(String[] args) {
Animal a = null;
System.out.println(a instanceof Animal); // always false for null
}
}
Login to try C/C++/Java/PHP code in the editor
Object Class Checks
चूंकि Java में हर class implicitly Object extend करती है, किसी भी non-null object instance को instanceof से Object के खिलाफ check करना हमेशा true return करेगा — यह अकेले शायद ही कभी उपयोगी है लेकिन यह illustrate करता है कि class hierarchy असल में कितनी गहरी जाती है।
उदाहरण: Object Class Checks
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
}
}
Login to try C/C++/Java/PHP code in the editor
Interface Checks
instanceof classes तक सीमित नहीं — आप यह भी जांच सकते हैं कि किसी object की class एक particular interface implement करती है या नहीं, जो आम है जब आपको confirm करना हो कि एक object एक capability support करता है (जैसे Comparable) इसे चाहने वाले methods call करने से पहले।
उदाहरण: Interface Checks
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
nullपरinstanceofcall करना, जोfalsereturn करता है, इसलिए check एक missing object छुपा सकता है।- Unrelated classes के साथ
instanceofउपयोग करना, जो एक compile error है। - एक downcast से पहले
instanceofcheck skip करना, इसलिए एकClassCastExceptionthrow होता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: