Undefined values: Difference between revisions

→‎{{header|Java}}: You can print a null type
(→‎{{header|Java}}: You can print a null type)
Line 121:
Java has a special null type, the type of the expression <code>null</code>, that can be cast to any reference type; in practice the null type can be ignored and <code>null</code> can be treated as a special literal that can be of any reference type. When a reference variable has the special value <code>null</code> it refers to no object, meaning that it is undefined.
<lang java>String string = null; // the variable string is undefined
System.out.println(string); //prints dereferencing "null" throwsto java.lang.NullPointerException</lang>std out
System.out.println(string.length()); // dereferencing null throws java.lang.NullPointerException</lang>
 
Variables of primitive types cannot be assigned the special value <code>null</code>, but there are wrapper classes corresponding to the primitive types (eg. <code>Boolean</code>, <code>Integer</code>, <code>Long</code>, <code>Double</code>, &c), that can be used instead of the corresponding primitive; since they can have the special value <code>null</code> it can be used to identify the variable as undefined.
Line 133 ⟶ 134:
i = 1;
}</lang>
 
 
=={{header|JavaScript}}==
Anonymous user