<< Git At Home | Home | Java 6 Update 10 Released? >>

Friday Java Quiz: It Can't Go Simpler Than This

Q: What will this program print? true? or false?

import java.io.*;

public class Foo {
  public static void main(String[] args) {
    Serializable bar = null;
    System.out.println(bar instanceof Serializable);
  }
}
Tags :


Re: Friday Java Quiz: It Can't Go Simpler Than This

Since nothing's been instanciated yet it will print false. Right?

Re: Friday Java Quiz: It Can't Go Simpler Than This

It prints "false" because the left hand side of the instanceof operator is null. It's a bit of a trick though because at first glance the first answer that comes to mind is that it prints "true".

Re: Friday Java Quiz: It Can't Go Simpler Than This

Or you can read the spec :)
JLS3 section 15.20.2, the second sentence:
"At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (§15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false."
Cheers,
Rémi

Re: Friday Java Quiz: It Can't Go Simpler Than This

I'm going to agree with the others that it prints false. The variable bar has no instance, instanceof is really a cast at the JVM bytecode level and it will fail.

Re: Friday Java Quiz: It Can't Go Simpler Than This

@Casper: Are you sure instanceof is a cast under the covers? I don't think so. You can cast `null` to any type you want - a feature used to resolve ambiguity when calling polymorphic methods with `null` arguments.

Re: Friday Java Quiz: It Can't Go Simpler Than This

There are separate bytecodes for instanceof and casting that have basically the same semantics as the source code operations. You can cast null to any type, but it is instanceof no type.

Re: Friday Java Quiz: It Can't Go Simpler Than This

prints false instanceof operator returns false on null values. null instanceof Xyz is always false :)

Re: Friday Java Quiz: It Can't Go Simpler Than This

I think the answer would be true, since assignment to null is not a problem for it. Since instanceof check whether it is really declared as that type or not. It hardly checks the value.

Re: Friday Java Quiz: It Can't Go Simpler Than This

Joshua Bloch in Effective Java: all objects must be unequal to null...Many classes have equals methods that guard against this with an explicit test for null:
@Override public boolean equals(Object o) {
    if (o == null)
        return false;
    ...
}
This test is unnecessary. To test its argument for equality, the equals method must first cast its argument to an appropriate type so its accessors may be invoked or its fields accessed. Before doing the cast, the method must use the instanceof operator to check that its argument is of the correct type:
@Override public boolean equals(Object o) {
    if (!(o instanceof MyType))
        return false;
    MyType mt = (MyType) o;
    ...
}
...the instanceof operator is specified to return false if its first operand is null, regardless of what type appears in the second operand [JLS, 15.20.2]. Therefore the type check will return false if null is passed in, so you don't need a separate null check.

Add a comment Send a TrackBack