Returns: The system console, if any, otherwise null. You can use it to enable what is called lazy initialization of fields, where a field would have its initial value of null until it's actually used, where it's replaced by the "real" value (which may be expensive to compute). For all reference types, the default value is null.Each class variable, instance variable, or array component is initialized with a default value when it is created:.It is also the default value (for variables that have them) for all reference types: JLS 4.12.5 Initial Values of Variables Note also that by contract, it also has this special property (from ):įor any non- null reference value x, x.equals(null) should return false. In Java, null = null (this isn't always the case in other languages).
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.Īs the JLS quote above says, in practice you can simply pretend that it's "merely a special literal that can be of any reference type". The null reference can always be cast to any reference type. The null reference is the only possible value of an expression of null type. 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. There is also a special null type, the type of the expression null, which has no name. What set does 'null' belong to? JLS 4.1 The Kinds of Types and Values
This means that for any type E and R, for any E o, where o = null, o instanceof R is always false. RelationalExpression instanceof ReferenceTypeĪt 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 to the ReferenceType without raising a ClassCastException. 15.20.2 Type Comparison Operator instanceof RelationalExpression: No, there is no type which null is an instanceof.