4

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
  • 2
    Is this an interview question?
    – finnw
    Aug 27, 2009 at 14:25

10 Answers 10

40

Is this a trick question?

Object obj = new Object() {{ System.out.println(false); System.exit(0); }};
System.out.println(obj instanceof Object);
3
  • Pardon my ignorance, what does the double-brace signify?
    – Everyone
    Aug 27, 2009 at 15:43
  • 2
    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). Aug 27, 2009 at 16:02
  • 4
    Unfortunately, this prints 'false' on creation, not when doing the instanceof check ;)
    – Jorn
    Aug 28, 2009 at 8:52
11

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...

0
9

This will never return false if obj is non-null

7

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

0

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

0
0

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.

0

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

Discussed here

1
  • "What should be obj so that the answer is false (Any other option other than null)"
    – ipingu
    Aug 27, 2009 at 14:52
0

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 java.lang.Object at the start of a method and a local class later:

public static void main(String[] args) {
    Object value = "42"; class Object {}
    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.

3
  • Why the mess with the generic class? This does the same: public static void main(String[] args) { Object v = (Object)"42"; class Object {} System.out.println(v instanceof Object); } Feb 12, 2011 at 19:53
  • @Atmocreations, it's a while since I wrote this but for some reason I assumed that wouldn't compile. I prefer your example (the simpler the better) & I've updated mine to match.
    – finnw
    Feb 12, 2011 at 20:37
  • hmm right. haven't seen that the post is quite old :D well didn't wanna criticize you. when I saw your code, I had to try it :P Compiles and runs from the command line, therefore this has nothing to do with eclipse. Feb 12, 2011 at 21:59
0

Old, old question but:

  • if obj is null
  • if you use jview back in late 90s and trying Variant (don't recall the package by heart).
0

It could also be an instance of a primitive object like int, double, boolean, etc. If do so, it'll probably return false as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.