Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 100
Creating Wrapper Classes
ОглавлениеEach primitive type has a wrapper class, which is an object type that corresponds to the primitive. Table 1.7 lists all the wrapper classes along with how to create them.
TABLE 1.7 Wrapper classes
Primitive type | Wrapper class | Wrapper class inherits Number ? | Example of creating |
---|---|---|---|
boolean | Boolean | No | Boolean.valueOf(true) |
byte | Byte | Yes | Byte.valueOf((byte) 1) |
short | Short | Yes | Short.valueOf((short) 1) |
int | Integer | Yes | Integer.valueOf(1) |
long | Long | Yes | Long.valueOf(1) |
float | Float | Yes | Float.valueOf((float) 1.0) |
double | Double | Yes | Double.valueOf(1.0) |
char | Character | No | Character.valueOf('c') |
There is also a valueOf()
variant that converts a String
into the wrapper class. For example:
int primitive = Integer.parseInt("123"); Integer wrapper = Integer.valueOf("123");
The first line converts a String
to an int
primitive. The second converts a String
to an Integer
wrapper class.
All of the numeric classes in Table 1.7 extend the Number
class, which means they all come with some useful helper methods: byteValue()
, shortValue()
, intValue()
, longValue()
, floatValue()
, and doubleValue()
. The Boolean
and Character
wrapper classes include booleanValue()
and charValue()
, respectively.
As you probably guessed, these methods return the primitive value of a wrapper instance, in the type requested.
Double apple = Double.valueOf("200.99"); System.out.println(apple.byteValue()); // -56 System.out.println(apple.intValue()); // 200 System.out.println(apple.doubleValue()); // 200.99
These helper methods do their best to convert values but can result in a loss of precision. In the first example, there is no 200
in byte
, so it wraps around to -56
. In the second example, the value is truncated, which means all of the numbers after the decimal are dropped. In Chapter 5, we apply autoboxing and unboxing to show how easy Java makes it to work with primitive and wrapper values.
Some of the wrapper classes contain additional helper methods for working with numbers. You don't need to memorize these; you can assume any you are given are valid. For example, Integer
has:
max(int num1, int num2), which returns the largest of the two numbers
min(int num1, int num2), which returns the smallest of the two numbers
sum(int num1, int num2), which adds the two numbers