Java Reference
In-Depth Information
Integer Literals
Before you use integer variables you need to understand how you write integer values of various types. As
I said earlier, a value of any kind in Java is referred to as a literal. So 1, 10.5, and “This is text” are all ex-
amples of literals.
Integer literals can be of type int or type long . Any integer literal that you specify as a sequence of
decimal digits is of type int by default. Thus 1, −9999, and 123456789 are all literals of type int . If you
want to define an integer literal of type long , you need to append an L to the value. The values 1L, −9999L,
and 123456789L are all of type long . You could use a lowercase letter l , but don't — it is too easily con-
fused with the digit 1 .
Very large integer literals with lots of digits can be difficult to read; you can make such literals more
readable by separating groups of digits using the underline character in the way you use a comma to separ-
ate groups of digits when writing numbers outside of programming. For example, you can write the value
1,234,567,890 as the literal 1234567899L or as the literal 1_234_567_890L. You could also use multiple
underscores in sequence if you feel it is necessary: 1__234__567__890L. Underscores can only appear in-
terior to a literal, so each set of one or more underlines in a literal must be bounded by digits both sides;
putting them anywhere else results in a compiler error message. As you will see later in this chapter, you can
use underscores in other kinds of numeric literals too.
You are perhaps wondering how you specify literals of type byte or short . You can't. Because of the
way integer arithmetic works in Java, they just aren't necessary in the main. You see a couple of instances
where an integer literal may be interpreted by the compiler as type byte or short later in this chapter, but
these situations are the exception.
You can write integer literals in four different ways:
• As decimal values that are numbers to base 10
• As hexadecimal values that are numbers to base 16
• As binary values that are numbers to base 2
• As octal values that are numbers to base 8
You have seen how to write decimal integer literals. Let's look at the others.
Hexadecimal Literals
Hexadecimal literals in Java have 0x or 0X in front of them and follow the usual convention of using the
letters A to F (or a to f ) to represent digits with values 10 to 15, respectively. In case you are a little rusty on
hexadecimal values, here are some examples:
0x100 is (1 × 16 2 ) + (0 × 16 1 ) + (0 × 16 0 ) which is 256 in decimal.
0x1234 is (1 × 16 3 ) + (2 × 16 2 ) + (3 × 16 1 ) + (4 × 16 0 ) which is 4660 in decimal.
0xDEAF is (13 × 16 3 ) + (14 × 16 2 ) + (10 × 16 1 ) + (15 × 16 0 ) which is 57007 in decimal.
0xCAB is (12 × 16 2 ) + (10 × 16 1 ) + (11 × 16 0 ) which is 3243 in decimal.
If you are not familiar with hexadecimal numbers, you can find an explanation of how these work in
Appendix B. All the hexadecimal literals in the preceding table are of type int . If you want to specify a
hexadecimal literal of type long , you must append L to the literal just as with decimal literals. For example,
0xFL is a hexadecimal literal that is equivalent to the decimal value 15. Of course, you can write a literal
such as 0xAABBCCD9L as 0xAABB_CCD9L. The underscore character here separates the hexadecimal
digits into groups of four. Each group of four hexadecimal digits corresponds to 2 bytes in memory. As with
Search WWH ::




Custom Search