Java Reference
In-Depth Information
Note Underscores cannot be placed at the beginning or end of a number, adjacent to
a decimal point or floating-point literal, prior to an F or L suffix, or in positions where a
string of digits is expected.
4-19. Declaring Binary Literals
Problem
You are working on an application that requires the declaration of binary numbers.
Solution
Make use of binary literals to make your code readable. The following code segment
demonstrates the use of binary literals:
int bin1 = 0b1100;
short bin2 = 0B010101;
short bin3 = (short) 0b1001100110011001;
System.out.println(bin1);
System.out.println(bin2);
System.out.println(bin3);
This will result in the following output:
12
21
-26215
How It Works
Binary literals became part of the Java language with the release of JDK 7. The types
byte , short , int , and long can be expressed using the binary number system.
This feature can help to make binary numbers easier to recognize in code. In order to
use the binary format, simply prefix the number with 0b or 0B .
Search WWH ::




Custom Search