{"id":1200,"date":"2022-02-25T16:42:57","date_gmt":"2022-02-25T16:42:57","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1200"},"modified":"2022-02-25T16:42:57","modified_gmt":"2022-02-25T16:42:57","slug":"java-instanceof-operator","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-instanceof-operator\/","title":{"rendered":"Java instanceof Operator"},"content":{"rendered":"\n<p>In this tutorial, you will learn about Java instanceof operator in detail with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>instanceof<\/code>&nbsp;operator in Java is used to check whether an object is an instance of a particular class or not.<\/p>\n\n\n\n<p>Its syntax is<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>objectName instanceOf className;<\/code><\/pre>\n\n\n\n<p>Here, if&nbsp;<var>objectName<\/var>&nbsp;is an instance of&nbsp;<var>className<\/var>, the operator returns&nbsp;<code>true<\/code>. Otherwise, it returns&nbsp;<code>false<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example: Java instanceof<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\n  public static void main(String&#91;] args) {\n\n    \/\/ create a variable of string type\n    String name = \"Programiz\";\n    \n    \/\/ checks if name is instance of String\n    boolean result1 = name instanceof String;\n    System.out.println(\"name is an instance of String: \" + result1);\n\n    \/\/ create an object of Main\n    Main obj = new Main();\n\n    \/\/ checks if obj is an instance of Main\n    boolean result2 = obj instanceof Main;\n    System.out.println(\"obj is an instance of Main: \" + result2);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>name is an instance of String: true\nobj is an instance of Main: true<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a variable&nbsp;<var>name<\/var>&nbsp;of the&nbsp;<code>String<\/code>&nbsp;type and an object&nbsp;<var>obj<\/var>&nbsp;of the&nbsp;<var>Main<\/var>&nbsp;class.<\/p>\n\n\n\n<p>Here, we have used the&nbsp;<code>instanceof<\/code>&nbsp;operator to check whether&nbsp;<var>name<\/var>&nbsp;and&nbsp;<var>obj<\/var>&nbsp;are instances of the&nbsp;<code>String<\/code>&nbsp;and&nbsp;<var>Main<\/var>&nbsp;class respectively. And, the operator returns&nbsp;<code>true<\/code>&nbsp;in both cases.<\/p>\n\n\n\n<p><strong>Note<\/strong>: In Java,\u00a0<code>String<\/code>\u00a0is a class rather than a primitive data type. To learn more, visit\u00a0Java String.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"inheritance\">Java instanceof during Inheritance<\/h2>\n\n\n\n<p>We can use the&nbsp;<code>instanceof<\/code>&nbsp;operator to check if objects of the subclass is also an instance of the superclass. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Java Program to check if an object of the subclass\n\/\/ is also an instance of the superclass\n\n\/\/ superclass\nclass Animal {\n}\n\n\/\/ subclass\nclass Dog extends Animal {\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of the subclass\n    Dog d1 = new Dog();\n\n    \/\/ checks if d1 is an instance of the subclass\n    System.out.println(d1 instanceof Dog);        \/\/ prints true\n\n    \/\/ checks if d1 is an instance of the superclass\n    System.out.println(d1 instanceof Animal);     \/\/ prints true\n  }\n}<\/code><\/pre>\n\n\n\n<p>In the above example, we have created a subclass&nbsp;<var>Dog<\/var>&nbsp;that inherits from the superclass&nbsp;<var>Animal<\/var>. We have created an object&nbsp;<var>d1<\/var>&nbsp;of the&nbsp;<var>Dog<\/var>&nbsp;class.<\/p>\n\n\n\n<p>Inside the print statement, notice the expression,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>d1 instanceof Animal<\/code><\/pre>\n\n\n\n<p>Here, we are using the&nbsp;<code>instanceof<\/code>&nbsp;operator to check whether&nbsp;<var>d1<\/var>&nbsp;is also an instance of the superclass&nbsp;<var>Animal<\/var>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"interface\">Java instanceof in Interface<\/h2>\n\n\n\n<p>The&nbsp;<code>instanceof<\/code>&nbsp;operator is also used to check whether an object of a class is also an instance of the interface implemented by the class. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Java program to check if an object of a class is also\n\/\/  an instance of the interface implemented by the class\n\ninterface Animal {\n}\n\nclass Dog implements Animal {\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of the Dog class\n    Dog d1 = new Dog();\n\n    \/\/ checks if the object of Dog\n    \/\/ is also an instance of Animal\n    System.out.println(d1 instanceof Animal);  \/\/ returns true\n  }\n}<\/code><\/pre>\n\n\n\n<p>In the above example, the&nbsp;<var>Dog<\/var>&nbsp;class implements the&nbsp;<var>Animal<\/var>&nbsp;interface. Inside the print statement, notice the expression,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>d1 instanceof Animal<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>d1<\/var>&nbsp;is an instance of&nbsp;<var>Dog<\/var>&nbsp;class. The&nbsp;<code>instanceof<\/code>&nbsp;operator checks if&nbsp;<var>d1<\/var>&nbsp;is also an instance of the interface&nbsp;<var>Animal<\/var>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: In Java, all the classes are inherited from the&nbsp;<code>Object<\/code>&nbsp;class. So, instances of all the classes are also an instance of the&nbsp;<code>Object<\/code>&nbsp;class.<\/p>\n\n\n\n<p>In the previous example, if we check,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>d1 instanceof Object<\/code><\/pre>\n\n\n\n<p>The result will be&nbsp;<code>true<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about Java instanceof operator in detail with the help of examples. The&nbsp;instanceof&nbsp;operator in Java is used to check whether an object is an instance of a particular class or not. Its syntax is Here, if&nbsp;objectName&nbsp;is an instance of&nbsp;className, the operator returns&nbsp;true. Otherwise, it returns&nbsp;false. Example: Java instanceof Output In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[484],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1200"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1200"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1200\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}