Java - What is the output: String instanceof ClassName

Question

What is the output from the following code

class Employee {
  private String name = "Unknown";

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}
public class Main {
  public static void main(String[] args) {
    String str = "test";
    if (str instanceof Employee) {
      System.out.println("Employee");
    }else{
      System.out.println("Not");
    }
  }
}


Click to view the answer

if (str instanceof Employee) { // A compile-time error
      System.out.println("Employee");
}else{
      System.out.println("Not");
}

Note

The use of the instanceof operator will generate a compiler error because the String class is not in the inheritance-chain of the Employee class.

Related Quiz