1. Floating Topic
  2. Floating Topic
  3. Assignment Operators
    1. Subtopic 1
  4. Relational Operators
    1. Always result in a boolean
  5. InstanceOf
    1. public static void main(String[] args) { String s = new String("foo"); if (s instanceof String) { System.out.print("s is a String") } }
    2. Even if the object being tested is not an actual instantiation of the class type on the right side of the operator, instanceof will still return true if the object being compared is assignment compatible with the type on the right class A { } class B extends A { public static void main (String [] args) { A myA = new B(); m2(myA); } public static void m2(A a) { if (a instanceof B) ((B)a).doBstuff(); // downcasting an A reference to a B reference } public static void doBstuff() { System.out.println("'a' refers to a B"); } } Prints : 'a' refers to a B
    3. You can't use the instanceof operator to test across two different class hierarchies. For instance, the following will NOT compile: class Cat { } class Dog { public static void main(String [] args) { Dog d = new Dog(); System.out.println(d instanceof Cat); } } Compilation fails—there's no way d could ever refer to a Cat or a subtype of Cat.