5 Literals
5 Literals
Identifiers: Identifiers are the names of variables, methods, classes, packages and
interfaces
Octal values are denoted in Java by a leading zero. Normal decimal numbers cannot
have a leading zero.
Thus, the seemingly valid value 09 will produce an error from the compiler, since 9
is outside of octal’s 0 to 7 range.
Beginning with JDK 7, you can also specify integer literals using binary. To do so,
prefix the value with 0b or 0B.
For example, this specifies the decimal value 10 using a binary literal: int x =
0b1010;
int x = 123_456_789;
the value given to x will be 123,456,789. The underscores will be ignored.
Hexadecimal floating-point literals are also supported, but they are rarely used.
They must be in a form similar to scientific notation, but a P or p, rather than an
E or e, is used.
For example, 0x12.2P2 is a valid floating-point literal. The value following the P,
called the binary exponent,
indicates the power-of-two by which the number is multiplied. Therefore, 0x12.2P2
represents 72.5.
Boolean literals are simple. There are only two logical values that a boolean value
can have, true and false.
The values of true and false do not convert into any numerical representation.
The true literal in Java does not equal 1, nor does the false literal equal 0.
As you may know, in some other languages, including C/C++, strings are implemented
as arrays of characters.
However, this is not the case in Java. Strings are actually object types.
Because Java implements strings as objects, Java includes extensive string-handling
capabilities that are both powerful
and easy to use.
The remainder a % b is defined such that (a / b) * b + a % b is always equal to a :
System.out.println(-15 % 2); // -1
System.out.println(15 % -2); // 1