In Java SE 7, literal representation in binary form has been added. You can easily use binary literals to represent numeric values like in decimal.
For example:
// An 8-bit byte value: byte aByte = 0b100001;// A 16-bit short value: short aShort = 0b10100101001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101;// A 64-bit long value (note the suffix "L" at the end) long aLong = 0b1010L;// The binary literal value starts with 0b or 0B and is case-insensitive int anInt2 = 0B101;
In Java SE 7, numerical representations of underscores as delimiters are also supported:
//Decimal form int anInt1 = 123_45_6;//Binary, hexadecimal, etc. also support int anInt2 = 0b10_0110_100; int anInt3 = 0xFF_EC_DE_5E;//Decimal form also supports float pi = 3.14_15F; double aDouble = 3.14_15;// Multiple underscores are connected int chain = 5______2____0;
However, the following writing forms are incorrect:
//The underscore cannot be placed at the end int x = 52_; //Error//The underscore cannot be adjacent to the decimal point (neither before or after the decimal point is adjacent to the decimal point) float pi1 = 3_.1415F; //Error float pi2 = 3._1415F; //Error//The underscore cannot be placed at the front of the suffix "L" or "F" float pi3 = 3.1415_F; //Error long aLong1 = 999_9999_L;//Error//The underscore cannot be placed between the prefix characters representing the cardinal int x5 = 0_x52; //Error
In addition, you should also pay attention to the following situation where the underscore is placed at the front:
int _52 = 120; //In Java, variable names cannot start with numbers, but can start with underscores int x = _52; //So, _52 here is not a literal form of a number, but a variable identifier (variable name)