Operátor instanceof vs metoda isInstance() v Javě

The instanceof operátor a isInstance() obě se používají pro kontrolu třídy objektu. Ale hlavní rozdíl nastává, když chceme třídu objektů zkontrolovat dynamicky, pak bude fungovat metoda isInstance(). Neexistuje způsob, jak to udělat pomocí operátoru instanceof.

Metoda isInstance je ekvivalentní operátoru instanceof. Metoda se používá v případě, že objekty jsou vytvářeny za běhu pomocí odrazu. Obecná praxe říká, že pokud má být typ zkontrolován za běhu, použijte metodu isInstance, jinak lze použít operátor instanceof.

Instance operátoru a Metoda isInstance() obě vrací booleovskou hodnotu. Metoda isInstance() je metoda třídy Class v jazyce Java, zatímco instanceof je operátor. 

Zvažte příklad:

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  );      }   }   

výstup: 

true 

Nyní, pokud chceme zkontrolovat třídu objektu za běhu, musíme použít isInstance() metoda.

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  );      }   }   

výstup: 

true false true 

Poznámka: Operátor instanceof vyvolá chybu při kompilaci (Nekompatibilní podmíněné typy operandů), pokud kontrolujeme objekt s jinými třídami, které nevytváří instanci.

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  );      }   }   

výstup:  

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

Musí si přečíst:

  • new operator vs newInstance() metoda v Javě  
  • Úvahy v Javě


Mohlo By Se Vám Líbit