understanding the difference between null and '\u0000' in java

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wangbingfengf98/article/details/102692514
public class MyClass {
    public static void main(String args[]) {
        char c = '\u000'; // [1]
        System.out.println("\u000:" + c);
    }
}

above code jdk is 1.8 or above 1.8. [1]'s error is

MyClass.java:3: error: illegal unicode escape
        char c = '\u000';

it should be '\u0000';

then output is:

:

For type char, the default value is the null character, that is, '\u0000'.

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.

 

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.


NullLiteral:
    null

A null literal is always of the null type (§4.1).

15.8.1. Lexical Literals

A literal (§3.10) denotes a fixed, unchanging value.

The following production from §3.10 is repeated here for convenience:


Literal:
    IntegerLiteral
    FloatingPointLiteral
    BooleanLiteral
    CharacterLiteral
    StringLiteral
    NullLiteral

The type of a literal is determined as follows:

  • The type of an integer literal (§3.10.1) that ends with L or l is long (§4.2.1).

    The type of any other integer literal is int (§4.2.1).

  • The type of a floating-point literal (§3.10.2) that ends with F or f is float and its value must be an element of the float value set (§4.2.3).

    The type of any other floating-point literal is double and its value must be an element of the double value set (§4.2.3).

  • The type of a boolean literal (§3.10.3) is boolean (§4.2.5).

  • The type of a character literal (§3.10.4) is char (§4.2.1).

  • The type of a string literal (§3.10.5) is String (§4.3.3).

  • The type of the null literal null (§3.10.7) is the null type (§4.1); its value is the null reference.

Evaluation of a lexical literal always completes normally.

 

4.2.1. Integral Types and Values

The values of the integral types are integers in the following ranges:

  • For byte, from -128 to 127, inclusive

  • For short, from -32768 to 32767, inclusive

  • For int, from -2147483648 to 2147483647, inclusive

  • For long, from -9223372036854775808 to 9223372036854775807, inclusive

  • For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

 

references:

1. https://stackoverflow.com/questions/12195628/understanding-the-difference-between-null-and-u000-in-java

2. https://docs.oracle.com/javase/specs/jvms/se6/html/Concepts.doc.html#22930

3. https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html

4. https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.7

5. https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.8.1

Guess you like

Origin blog.csdn.net/wangbingfengf98/article/details/102692514