1

As per JLS 4.1 :

There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.

Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.

The null reference is the only possible value of an expression of null type.

The null reference can always undergo a widening reference conversion to any reference type.

In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

If null could really be of any reference type then why IS-A test fails? i.e.

null instanceof Object      //returns false

OR

null instanceof String      //returns false
maximus335
  • 674
  • 1
  • 5
  • 19
  • 2
    `instanceof` returns `false` if its first operand is `null`, always. – Maroun Feb 11 '15 at 08:10
  • In your examples, null could be of any type. I'm not entirely sure, but if you change them to `(Object)null instanceof Object` and `(String)null instanceof String` or `Object o = null; o instanceof Object` and `String s = null; s instanceof String` it should return true. – Kevin Cruijssen Feb 11 '15 at 08:11
  • It was already answered: http://stackoverflow.com/questions/19336608/what-does-null-mean/19336626#19336626 – Nir Alfasi Feb 11 '15 at 08:11
  • `null` isn't even an instance, it's specifically the lack of an instance. So why would it be an instance of anything? – harold Feb 11 '15 at 08:11
  • 1
    This might prove to be a good read: http://stackoverflow.com/questions/2707322/what-is-null-in-java – Stultuske Feb 11 '15 at 08:12
  • http://stackoverflow.com/questions/2707322/what-is-null-in-java similar like this – Krutik Jayswal Feb 11 '15 at 08:12
  • `null` isn't really an _instance_ of a type, though it can be assigned to references of that type. – Louis Wasserman Feb 11 '15 at 08:20
  • @Kevin As per JLS widening is always possible with null. Then why it would not widen (without doing it explicitly) and return true in this expression itself `null instanceof Object` ? – maximus335 Feb 11 '15 at 08:21
  • @maximus335 Because null in `null instanceof Object` isn't a reference type. For example, when we have `Object o = new String("test");`, `Object` is the reference type and `String` is the object type. In your example, null is lacking a reference type, so therefore the `instanceof` will always fail. (At least that's how I've learned it.) I would also suggest in reading the post in the link @Stultuske provided. – Kevin Cruijssen Feb 11 '15 at 08:32
  • 1
    @kevin I just tried running code from your previous comment i.e. `(String)null instanceof String` it returns false not true. Please correct it. – maximus335 Feb 11 '15 at 08:37
  • @maximus335 Ah, I stand corrected. It turns out null will always return false, so I learned something new myself thanks to your question. `String s = null; s instanceof String` also returns false so it seems ([ideone test](http://ideone.com/dGJRcC)). – Kevin Cruijssen Feb 11 '15 at 08:51

2 Answers2

2

Consider null as the exact opposite of Object . Any value can be assigned to an Object, likewise any reference type can be set to null. instanceof is always true if RHS is Object, likewise instanceof is always false if null is the LHS. Logically, if a language has a structure which is everything (Object), it should also have a structure to define nothing (null).

Now, coming back to your original question. In java, suppose you have

Object o = null, its equivalent byte code instruction will be.

0: aconst_null --> Null reference is pushed onto the stack
1: astore_1    --> store a reference into local variable 1 (i.e, Object o)

So under the hood null is handled in a different way. System.out.println(null instanceof Object); will give false because according to the semantics of the language null is nothing and cannot have any instance. It is a mere placeholder to represent that a reference points to a valid but junk value.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

object instanceof Type checks the runtime type of the object referred to by the reference object. For example,

 Animal animal = new Dog();
 animal instanceof Dog; // returns true

instanceof looks up the actual object referred to by the reference and tests its runtime type. null does not refer to an actual object on the heap, so of course null instanceof Type has to return false.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413