Get startedGet started for free

Wrapper classes

1. Wrapper classes

Here, we'll explore Java wrapper classes, their methods and fields, how they wrap primitive values, and how they enable object-like behavior for primitives.

2. Java 8 primitives

Java's primitive types are predefined by Java and determine how much memory a variable needs to store data. They are the building blocks for all other data types in Java. Unlike objects, primitives do not have fields or methods - they simply hold basic values like numbers, characters, or booleans.

3. Wrapper classes

Java wrappers are classes that allow us to treat a primitive like an object. There is a wrapper for each of the 8 primitive types.

4. Wrapping primitives

Wrapper instances contain (or wrap) a value. The value is of the associated primitive type. So Integer wraps `ints`.

5. Wrapper details

Wrapper classes include extra fields and methods. For example, many numeric wrappers have a field for the maximum allowed value and methods to convert strings into numbers. Since they are part of Java's core language, no imports are needed. We'll explore these features soon.

6. Creating wrapper objects

To create a wrapper instance, assign a primitive value to a variable of the corresponding wrapper type using the = operator. This automatically wraps the primitive inside the object. Since wrappers are objects, they can also be set to null, meaning the variable exists but doesn't hold a wrapped value.

7. Using wrapper objects

Each wrapper class has a rich set of methods and fields. There are methods for printing their wrapped value, and for getting the wrapped primitive value. Other methods, like `doubleValue` and `toString`, transform their values to other types. The `compareTo` method compares the wrapped primitive of one object to another. It returns 0 if the values are equal, a negative number if the argument's value is greater, and a positive number if the argument's value is smaller.

8. Wrapper Static Methods

Wrapper classes also come with a collection of static methods. Static methods belong to the class rather than to any instance and therefore are called on the class. Methods like `sum` and `remainderUnsigned` perform math operations on primitives. Additional methods like `parseInt` and `parseBoolean` extract primitives from strings.

9. Wrapper Static Fields

In addition to static methods, wrapper classes often include static fields, which follow the convention of being written in all caps. Wrapper classes like `Integer` shown here contain fields to know the maximum and minimum value for the associated primitive type. Wrapped value options of true and false are static fields on Boolean.

10. Wrapper Static Fields

Character provides static fields that define the Unicode characters for space, line separator and more.

11. Interesting Wrapper Methods

A complete review of all the methods of all the wrapper classes is beyond the scope here. Here are some interesting methods of some wrapper classes.

12. Why Wrappers?

Overall, wrappers serve an important purpose in allowing a primitive to be treated like an object. Wrappers also allow methods and fields to be associated to the type. As we'll learn later in this course, wrappers allow primitives to be put in Java collection objects.

13. Wrappers can be null

A wrapper instance or static variable can also be null and its value assigned later. Primitive instance or static variables always have a default value, even when not explicitly assigned.

14. Let's practice!

Let's create some wrapper objects and use some static methods in our exercise.