Java Reference
In-Depth Information
Integer binary literals start with 0b and may, of course, only feature the digits 1 or 0.
As binary literals can be very long, underscores are often used as part of a binary
literal. The underscore character is ignored whenever it is encountered in any
numerical literal—it's allowed purely to help with readability of literals.
Java also supports octal (base-8) integer literals. These literals begin with a leading 0
and cannot include the digits 8 or 9. They are not often used and should be avoided
unless needed. Legal hexadecimal, binary, and octal literals include:
0xff // Decimal 255, expressed in hexadecimal
0377 // The same number, expressed in octal (base 8)
0 b0010_1111 // Decimal 47, expressed in binary
0xCAFEBABE // A magic number used to identify Java class files
Integer literals are 32-bit int values unless they end with the character L or l , in
which case they are 64-bit long values:
1234 // An int value
1234L // A long value
0xff L // Another long value
Integer arithmetic in Java never produces an overflow or an underflow when you
exceed the range of a given integer type. Instead, numbers just wrap around. For
example:
byte b1 = 127 , b2 = 1 ; // Largest byte is 127
byte sum = ( byte )( b1 + b2 ); // Sum wraps to -128, the smallest byte
Neither the Java compiler nor the Java interpreter warns you in any way when this
occurs. When doing integer arithmetic, you simply must ensure that the type you
are using has a sufficient range for the purposes you intend. Integer division by zero
and modulo by zero are illegal and cause an ArithmeticException to be thrown.
Each integer type has a corresponding wrapper class: Byte , Short , Integer , and
Long . Each of these classes defines MIN_VALUE and MAX_VALUE constants that
describe the range of the type. The classes also define useful static methods, such as
Byte.parseByte() and Integer.parseInt() , for converting strings to
integer values.
Floating-Point Types
Real numbers in Java are represented by the float and double data types. As shown
in Table 2-1 , float is a 32-bit, single-precision floating-point value, and double is a
64-bit, double-precision floating-point value. Both types adhere to the IEEE
754-1985 standard, which specifies both the format of the numbers and the behav‐
ior of arithmetic for the numbers.
Floating-point values can be included literally in a Java program as an optional
string of digits, followed by a decimal point and another string of digits. Here are
some examples:
Search WWH ::

Custom Search