Opérateur instanceof vs méthode isInstance() en Java

Le instance de opérateur et estInstance() Les deux méthodes sont utilisées pour vérifier la classe de l’objet. Mais la principale différence vient lorsque nous voulons vérifier la classe d’objets de manière dynamique, alors la méthode isInstance() fonctionnera. Il n'y a aucun moyen de le faire par l'opérateur instanceof.

La méthode isInstance est équivalente à l’opérateur instanceof. La méthode est utilisée dans le cas où des objets sont créés au moment de l'exécution à l'aide de la réflexion. La pratique générale indique que si le type doit être vérifié au moment de l'exécution, utilisez la méthode isInstance, sinon l'opérateur instanceof peut être utilisé.

L'opérateur instanceof et La méthode isInstance() renvoie toutes deux une valeur booléenne. La méthode isInstance() est une méthode de classe Class en Java tandis que instanceof est un opérateur. 

Prenons un exemple :

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

Sortir: 

true 

Maintenant, si nous voulons vérifier la classe de l'objet au moment de l'exécution, nous devons utiliser estInstance() méthode.

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

Sortir: 

true false true 

Note: L'opérateur instanceof génère une erreur de compilation (types d'opérandes conditionnels incompatibles) si nous vérifions l'objet avec d'autres classes qu'il n'instancie pas.

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

Sortir :  

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

Doit lire :

  • nouvel opérateur vs méthode newInstance() en Java  
  • Réflexions en Java