1

As I understood null is instance of T for all reference type T.

Question 1. What's defenition of null? How does null represents in memory?

Consider the following case: We defined class

public class MyClass{
    Integer i;
}

Now null can be cast to MyClass, but I don't explicitly define that.

Question 2. From what compiler will be know that null can be cast to MyClass?

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
St.Antario
  • 26,175
  • 41
  • 130
  • 318

4 Answers4

11

null is actually not instanceof anything!

The instanceof operator from the Java Language Specification (§15.20.2):

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.

4.1. The Kinds of Types and Values

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).

Type: PrimitiveType ReferenceType 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.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
10

Formally, null is a singleton member of the null type, which is defined to be the subtype of every other Java type.

null is a reference type and its value is the only reference value which doesn't refer to any object. Therefore there is no representation of null in memory. The binary value of a reference-typed variable whose value is null is simply zero (all zero bits). Even though this is not explicitly specified, it follows from the general initialization semantics of objects and any other value would cause major problems to an implementation.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • _Formally, null is a singleton member of the null type, which is defined to be the subtype of every other Java type._ Excelent!!! I'm understand this. But what is `null` type? At the duplicate topic write that _null type has no name_. What does mean that? I cant find info in the official docs about "anonymous argument type". – St.Antario Oct 12 '13 at 17:00
  • The `null` type has no representation in Java the language and cannot be referred to in any way: it exists only at the specification level. Theoretically, the concept of the `null` type could be removed and the language rules rewritten to have the same end effect, but in different words. That would, however, make the wording more messy and repetitive. – Marko Topolnik Oct 12 '13 at 17:28
  • [*Section 3.4 last line*](http://docs.oracle.com/javase/specs/jvms/se5.0/html/Overview.doc.html): `The Java virtual machine specification does not mandate a concrete value encoding null.` it doesn't necessarily encoded as `zero` - that's implementation specific. – Nir Alfasi Oct 13 '13 at 14:35
  • Formally from JLS: "3.10.7. The Null Literal The null type has one value, the null reference, represented by the null literal null, which is formed from ASCII characters. " – Oleksii Kyslytsyn Jul 13 '17 at 09:58
2
  1. Null means nothing. No object. At all. A null value can be assigned to an object reference of any type.

  2. Null can be cast to any type as it can be assigned to an object reference of that type. It, in a way, can act as every type, except for the fact that it is useless when trying to do field/method access(where a NullPointerException will be thrown) and is not every type. Note that null instanceof Foo is always false, no matter what foo is.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • Small note. JLS:"The Java programming language is also a strongly typed language..."; "The types of the Java programming language are divided into two categories: primitive types and reference types.". So your sentence is incorrect, the correct one is: "...Null can be cast to any !REFERENCE! type." Test: int j = null. Which is compilation issue. – Oleksii Kyslytsyn Jul 13 '17 at 09:55
2

In Java, a variable is a reference to an object. A null value thus indicates an unset reference (i.e. a reference to nothing).

You can see variables as containers(*), inside which you can put an object of a given type, when the variable is null, it means your container is empty. It's obvious that any container can be empty (regardless of the type of object it's supposed to host), you only need to put nothing in it (or removing what was inside). Nontheless, the void is not an object of any type, it's just what's left when you take the content out of a container.

(*) I do know that containers are not a good abstraction for reference values, but they were good for this specific explanation.

Giulio Franco
  • 3,170
  • 15
  • 18