tags:

views:

363

answers:

8

Consider

 Object obj = ....;
 System.out.println(obj instanceof Object);

What should obj be so that the answer is false (Any other option other than null)

+1  A: 

Every object descends from 'Object'. Your statement will always be true.

Crimson
This is incorrect - It will return false if obj is null.
Adamski
That is part of the question
Crimson
+3  A: 

Everything extends Object, so you will always get true here (unless obj is null).

DaveJohnston
+9  A: 

This will never return false if obj is non-null

oxbow_lakes
A: 

None AFAIK. Did you get this as an interview question?

By definition, it should return true if you can cast the variable into an Object, and all non-nulls should be convertible. Perhaps there is some trick with generics, but I doubt that.

Uri
+24  A: 

Is this a trick question?

Object obj = new Object() {{ System.out.println(false); System.exit(0); }};
System.out.println(obj instanceof Object);
Tom Hawtin - tackline
a genius answer
oxbow_lakes
that's famous
ipingu
Pardon my ignorance, what does the double-brace signify?
Everyone
The double brace idiom. The outer brace is the syntax for an anonymous inner class (in this case not defining any methods). The inner brace is an instance initialiser (becomes part of the constructor).
Tom Hawtin - tackline
good one! :-)
Ajay
Unfortunately, this prints 'false' on creation, not when doing the instanceof check ;)
Jorn
Really a gr8 answer now i am ur fan
Rahul Garg
A: 

The only way to make it false is not to give it an object, give it a null reference

Discussed here

Narayan
"What should be obj so that the answer is false (Any other option other than null)"
ipingu
A: 

Obviously if Object refers to java.lang.Object (as defined by the boot classloader) this is not possible as every class in the process must descend from java.lang.Object

However you can define something else called Object in an inner scope, hiding java.lang.Object.

Here's an example where the name Object refers to a type parameter and a local class, at different points within the same method:

public class MySillyClass<Object> {
    public static void main(String[] args) {
     new MySillyClass<Integer>().doWeirdStuff();
    }

    public void doWeirdStuff() {
     Object value = (Object) "42"; class Object<E> {}
     System.out.println(value instanceof Object);
    }
}

This is a slight cheat because the declaration of value is not a single statement but a statement followed by a local class definition.

I tested this in Eclipse 3.5.0, but I wouldn't be surprised if other compilers behave differently with a pathological example like this.

finnw
+6  A: 

This will print false:

public final class Foo {
    static private final class Object {
    }

    static public void main(String[] args)
    {
        java.lang.Object o = new java.lang.Object();
        System.out.println(o instanceof Object);
    }
}

It's not quite what you asked for, but the best I could think of...

Jon Bright