operator instancjiof vs metoda isInstance() w Javie

The instancja operatora i isInstance() Obie metody służą do sprawdzania klasy obiektu. Ale główna różnica pojawia się, gdy chcemy dynamicznie sprawdzić klasę obiektów, wówczas zadziała metoda isInstance(). Nie możemy tego zrobić za pomocą operatora instancjiof.

Metoda isInstance jest odpowiednikiem operatora instancjiof. Metodę stosuje się w przypadku tworzenia obiektów w czasie wykonywania z wykorzystaniem odbicia. Ogólna praktyka mówi, że jeśli typ ma być sprawdzany w czasie wykonywania, należy użyć metody isInstance, w przeciwnym razie można użyć operatora instancjiof.

Instancja operatora i Metoda isInstance() zwraca wartość logiczną. Metoda isInstance() jest metodą klasy Class w Javie, podczas gdy instancjaof jest operatorem. 

Rozważ przykład:

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

Wyjście: 

true 

Teraz, jeśli chcemy sprawdzić klasę obiektu w czasie wykonywania, musimy użyć 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  );      }   }   

Wyjście: 

true false true 

Notatka: operator instancjiof zgłasza błąd czasu kompilacji (niezgodne typy operandów warunkowych), jeśli sprawdzamy obiekt z innymi klasami, których nie tworzy.

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

Wyjście :  

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

Musisz przeczytać:

  • nowy operator vs metoda newInstance() w Javie  
  • Odbicia w Javie