Saturday, September 20, 2014

instanceof


import java.util.Date;

public class InstanceOfTest {
  
    // if no extend or implementation relationship between two objects, it will give compile error.
  
    public static void main(String[] args){
        boolean b = false;
      
        b = "a" instanceof String;
        System.out.println(b);
      
        b = new String() instanceof String;
        System.out.println(b);
      
        b = new Object() instanceof String; // object is not instanceof String
        System.out.println(b);
      
        //b = 'A' instanceof Character;
        // compile error. 'A' is simple variable and Character is object
      
        b = null instanceof String; // null is not instanceof any types
        System.out.println(b);
      
      
        String s = (String) null; // s is still null even after conversion
        System.out.println(s);
      
        b = (String) null instanceof String; // null is still null even after conversion
        System.out.println(b);
      
        //b = new Date() instanceof String;
        // compile error.
        // No extend or implementation relationship between Date and String so compile error.
    }

}



OUTPUT
true
true
false
false
null
false



No comments:

Post a Comment