Java'da exampleof operatörü vs isInstance() Yöntemi

örneği operatör ve isInstance() yöntemin her ikisi de nesnenin sınıfını kontrol etmek için kullanılır. Ancak asıl fark, nesnelerin sınıfını dinamik olarak kontrol etmek istediğimizde ortaya çıkar, o zaman isInstance() yöntemi işe yarar. Bunu exampleof operatörüyle yapmamızın hiçbir yolu yok.

isInstance yöntemi, exampleof operatörüne eşdeğerdir. Bu yöntem, nesnelerin çalışma zamanında yansıma kullanılarak oluşturulması durumunda kullanılır. Genel uygulama, türün çalışma zamanında kontrol edilmesi gerekiyorsa isInstance yöntemini kullandığını, aksi takdirde Instanceof operatörünün kullanılabileceğini söylüyor.

Örnek operatörü ve isInstance() yönteminin her ikisi de bir boole değeri döndürür. isInstance() yöntemi, Java'daki Class sınıfının bir yöntemidir, Instanceof ise bir operatördür. 

Bir Örnek düşünün:

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

Çıkış: 

true 

Şimdi eğer nesnenin sınıfını çalışma zamanında kontrol etmek istiyorsak o zaman şunu kullanmalıyız: isInstance() Yöntem.

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

Çıkış: 

true false true 

Not: Instanceof operatörü, nesneyi başlatmadığı diğer sınıflarla kontrol edersek derleme zamanı hatası (Uyumsuz koşullu işlenen türleri) atar.

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

Çıkış :  

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

Okumalısınız:

  • Java'da yeni operatör vs newInstance() yöntemi  
  • Java'daki yansımalar