Operatora instanceof vs isInstance() metode Java

The instanceof operators un isInstance() Abas metodes tiek izmantotas objekta klases pārbaudei. Bet galvenā atšķirība rodas, ja mēs vēlamies dinamiski pārbaudīt objektu klasi, tad darbosies metode isInstance (). Mēs to nevaram izdarīt, izmantojot operatoru instanceof.

Metode isInstance ir līdzvērtīga instanceof operatoram. Metode tiek izmantota, ja objekti tiek izveidoti izpildes laikā, izmantojot refleksiju. Vispārējā prakse saka, ka, ja tips ir jāpārbauda izpildlaikā, izmantojiet metodi isInstance, pretējā gadījumā var izmantot instanceof operatoru.

Operatora instance un Abas metodes isInstance() atgriež Būla vērtību. Metode isInstance () ir java klases Class metode, savukārt instanceof ir operators. 

Apsveriet piemēru:

Java
   // Java program to demonstrate working of   // instanceof operator   public     class   Test   {      public     static     void     main  (  String  []     args  )      {      Integer     i     =     new     Integer  (  5  );      // prints true as i is instance of class      // Integer      System  .  out  .  println  (  i     instanceof     Integer  );      }   }   

Izvade: 

true 

Tagad, ja mēs vēlamies pārbaudīt objekta klasi izpildes laikā, mums ir jāizmanto isInstance() metodi.

Java
   // Java program to demonstrate working of isInstance()   // method   public     class   Test     {      // This method tells us whether the object is an      // instance of class whose name is passed as a      // string 'c'.      public     static     boolean     fun  (  Object     obj       String     c  )      throws     ClassNotFoundException      {      return     Class  .  forName  (  c  ).  isInstance  (  obj  );      }      // Driver code that calls fun()      public     static     void     main  (  String  []     args  )      throws     ClassNotFoundException      {      Integer     i     =     new     Integer  (  5  );      // print true as i is instance of class      // Integer      boolean     b     =     fun  (  i       'java.lang.Integer'  );      // print false as i is not instance of class      // String      boolean     b1     =     fun  (  i       'java.lang.String'  );      // print true as i is also instance of class      // Number as Integer class extends Number      // class      boolean     b2     =     fun  (  i       'java.lang.Number'  );      System  .  out  .  println  (  b  );      System  .  out  .  println  (  b1  );      System  .  out  .  println  (  b2  );      }   }   

Izvade: 

true false true 

Piezīme: Operators instanceof rada kompilēšanas laika kļūdu (nesaderīgi nosacījuma operandu veidi), ja mēs pārbaudām objektu ar citām klasēm, kuras tas neveido.

Java
   public     class   Test     {      public     static     void     main  (  String  []     args  )      {      Integer     i     =     new     Integer  (  5  );      // Below line causes compile time error:-      // Incompatible conditional operand types      // Integer and String      System  .  out  .  println  (  i     instanceof     String  );      }   }   

Izvade:  

9: error: incompatible types: Integer cannot be converted to String System.out.println(i instanceof String); ^ 

Jālasa:

  • jauns operators vs newInstance() metode Java  
  • Pārdomas Java valodā