Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training

class A {
  static class B { }
  static class C extends A { }
  
  public static void main(String a[]) {
    new A().go();  
  }
  
  public void go() {
    pr(this instanceof C);
    pr(new C() instanceof A);
    pr(new Object() instanceof A);
    pr(this instanceof Object);
  }
  
  static void pr(Object o) {
    System.out.print(o + " ");
  }
}


Output: false true false true


Is it because of Autoboxing ?

Hi Shashank,


There's nothing about autoboxing going here. Autoboxing is the thing that converts Integer objects to int values, Double objects to double values and so on.


To answer your question, can you please let me know what you think should be the output, and what your reason is for that?

Hi ,


I am expecting runtime exception . Because instanceof operator returns either true or false and these values are primitive so how can we pass it to Object ?

Oh! You are after the method parameter! In that case, yes, it's auto-boxing taking place. Similar to the idea you can pass a boolean[/b] primitive value to a [h]Boolean type reference, the primitive boolean value returned from the instanceof operation can be passed to a method which takes a Boolean parameter!

ExamLab © 2008 - 2024