ТОП просматриваемых книг сайта:
OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Читать онлайн.Название OCP Oracle Certified Professional Java SE 17 Developer Study Guide
Год выпуска 0
isbn 9781119864592
Автор произведения Jeanne Boyarsky
Жанр Программы
Издательство John Wiley & Sons Limited
TABLE 1.6 Primitive types
Keyword | Type | Min value | Max value | Default value | Example |
---|---|---|---|---|---|
boolean
|
true or false
|
n/a | n/a |
false
|
true
|
byte
|
8-bit integral value | -128 | 127 |
0
|
123
|
short
|
16-bit integral value | -32,768 | 32,767 |
0
|
123
|
int
|
32-bit integral value | -2,147,483,648 | 2,147,483,647 |
0
|
123
|
long
|
64-bit integral value | -263 | 263 – 1 |
0L
|
123L
|
float
|
32-bit floating-point value | n/a | n/a |
0.0f
|
123.45f
|
double
|
64-bit floating-point value | n/a | n/a |
0.0
|
123.456
|
char
|
16-bit Unicode value | 0 | 65,535 |
\u0000
|
'a'
|
Is String a Primitive?
No, it is not. That said, String
is often mistaken for a ninth primitive because Java includes built-in support for String
literals and operators. You learn more about String
in Chapter 4, but for now, just remember it's an object, not a primitive.
There's a lot of information in Table 1.6. Let's look at some key points:
The byte, short, int, and long types are used for integer values without decimal points.
Each numeric type uses twice as many bits as the smaller similar type. For example, short uses twice as many bits as byte does.
All of the numeric types are signed and reserve one of their bits to cover a negative range. For example, instead of byte covering 0 to 255 (or even 1 to 256) it actually covers -128 to 127.
A float requires the letter f or F following the number so Java knows it is a float. Without an f or F, Java interprets a decimal value as a double.
A long requires the letter l or L following the number so Java knows it is a long. Without an l or L, Java interprets a number without a decimal point as an int in most scenarios.
You won't be asked about the exact sizes of these types, although you should have a general idea of the size of smaller types like byte
and short
. A common question among newer Java developers is, what is the bit size of boolean
? The answer is, it is not specified and is dependent on the JVM where the code is being executed.
Signed and Unsigned: short and char
For the exam, you should be aware that short
and char
are closely related, as both are stored as integral types with the same 16-bit length. The primary difference is that short
is signed, which means it splits its range across the positive and negative integers. Alternatively, char
is unsigned, which means its range is strictly positive, including 0
.
Often, short
and char
values can be cast to one another because the underlying data size is the same. You learn more about casting in Chapter 2, “Operators.”
Writing Literals
There are a few more things you should know about numeric primitives. When a number is present in the code, it is called a literal. By default, Java assumes you are defining an int
value with a numeric literal. In the following example, the number listed is bigger than what fits in an int
. Remember, you aren't expected to memorize the maximum value for an int
. The exam will include it in the question if it comes up.
long max = 3123456789; // DOES NOT COMPILE
Java complains the number is out of range. And it is—for an int
. However, we don't have an int
. The solution is to add the character L
to the number:
long max = 3123456789L; // Now Java knows it is a long
Alternatively, you could add a lowercase l
to the number. But please use the uppercase L
. The lowercase l
looks like the number 1
.
Another way to specify numbers is to change the “base.” When you learned how to count, you studied the digits 0–9. This numbering system is called base 10 since there are 10 possible values for each digit. It is also known as the decimal number system. Java allows you to specify digits in several other formats:
Octal (digits 0–7), which uses the number 0 as a prefix—for example, 017.
Hexadecimal (digits 0–9 and letters A–F/a–f), which uses 0x or 0X as a prefix—for example, 0xFF, 0xff, 0XFf. Hexadecimal is case insensitive, so all of these examples mean the same value.
Binary (digits 0–1), which uses the number 0 followed