# 4.7 Wrapper Classes - Integer and Double

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

A **wrapper class** is a class that wraps (encloses) around a primitive data type and gives it an object appearance. The wrapper classes are part of the `java.lang` package, which is imported by default into all Java programs. The `Integer` class and `Double` class are **wrapper classes** that create objects from primitive types of `int` and `double` respectively. Sometimes you need to create a wrapped object for a primitive type so that you can give it to a method that is expecting an object, or to put it in a collection like an `ArrayList` that we will see in the next lesson. The wrapper classes are also used to convert strings to primitive data types and to convert primitive data types to strings which is useful when using the `Scanner` class with input.

## Creating `Integer` and `Double` Objects

Here are some examples of creating `Integer` and `Double` objects. In Java version 7, you can use a constructor like `new Integer(2)` to create an object with the value 2 in it. In Java 9 and later, you can just use `Integer i = 2;` to create an object with the value 2 wrapped in it. `Integer` and `Double` objects are immutable, meaning once the objects are created, their attributes (their values) cannot be changed.

```java
// in older versions of Java
Integer i = new Integer(2); // create an object with 2 in it
Double d = new Double(3.5); // create an object with 3.5 in it

// in newer versions of Java (9+)
Integer i = 2;
Double d = 3.5;
```

These wrapper classes are also useful because they have some special values (like the minimum and maximum values for the type) and methods that you can use. Try the following code to see the minimum and maximum values possible for the type int.

### Exercise 1

What’s the minimum and maximum numbers for an int? What happens if you go beyond these limits with - 1 or + 1?

> Open the `01-lcmm1` folder and run `Test1.java`. It is an example, so it has no tests.

The int type in Java can be used to represent any whole number from -2147483648 to 2147483647. Why those numbers? Integers in Java are represented in 2’s complement binary and each integer gets 32 bits of space. In 32 bits of space with one bit used to represent the sign you can represent that many values. Why is there one more negative number than positive number? It is because 0 is considered a positive number.

What do the last two lines print out? Did this surprise you? Java will actually return the maximum integer value if you try to subtract one from the minimum value. This is called **underflow**. And, Java will return the minimum integer value if you try to add one to the maximum. This is called **overflow**. It is similar to how odometers work – in a really old car that reaches the maximum miles possible on the odometer, the odometer rolls over back to 0, the minimum value. In Java, any int value that surpasses 32 bits gets rolled over, so that the Integer.MAX_VALUE 2147483647 incremented (+1) returns -2147483648 which is the Integer.MIN_VALUE.

When would you ever use Integer.MIN_VALUE or Integer.MAX_VALUE? They are handy if you want to initialize a variable to the smallest possible value and then search a sequence of values for a larger value.

## Autoboxing and Unboxing

**Autoboxing** is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an `int` to an `Integer` and a `double` to a `Double`. The Java compiler applies autoboxing when a primitive value is passed as a parameter to a method that expects an object of the corresponding wrapper class or assigned to a variable of the corresponding wrapper class. Here’s an example of autoboxing.

```java
Integer i = 2;
Double d = 3.5;
```

**Unboxing** is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double. The Java compiler applies unboxing when a wrapper class object is passed as a parameter to a method that expects a value of the corresponding primitive type or assigned to a variable of the corresponding primitive type. Here’s an example of unboxing:

```java
Integer i = 2;  // autoboxing - wrap 2
int number = i; // unboxing - back to primitive type
```

> Check your understanding: Drag the definition from the left and drop it on the correct word on the right. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/topic-4-7-wrapper-classes.html#WrapperClasses

## Parsing Strings to Numbers

The `Integer` and `Double` classes have methods called `Integer.parseInt` and `Double.parseDouble` that can be used to convert strings to numbers. They are often used with the `Scanner` class to convert input which is read in as a `String` into an `int` or `double` so that you can create arithmetic expressions (do math) or create logical conditions that test the values against other numbers using relational operators like `<` and `>`. These methods are listed on the Java Quick Reference Sheet provided during the AP exam.

- `static int parseInt(String s)` returns the `String` argument as a signed `int`.
- `static double parseDouble(String s)` returns the `String` argument as a signed `double`.

Here’s an example of using these methods.

### Exercise 2

Run the code to see useful parse methods in the Integer and Double wrapper classes.

> Open the `02-parseMethods` folder and run `Test2.java`. It is an example, so it has no tests.

### Exercise 3

Find and fix the bugs below to use the correct Integer and Double methods and variables.

> Open the `03-wrapperDebug` folder and edit `Debug.java`. Press **Run** to try it; **Run Tests** checks it (3 tests).

## Coding Challenge: Pokemon Speed

In the last lesson, we read in a file of Pokemon data. In this exercise, we will read in the file and calculate the average Pokemon speed and find the Pokemon with the highest speed. The speed is the 8th column in the file; when a row of data is split into an array with the index starting at 0, the speed will have the 7th index. We will use the `Integer.parseInt` method to convert the speed from a string to an integer so we can compare the speeds.

### Exercise 4

This program reads in each line from the pokemon file into a String array of lines. Complete the `findMaxSpeed` and `findAverageSpeed` methods below.

> Open the `04-challenge-pokespeed` folder and edit `PokeSpeed.java`. Press **Run** to try it; **Run Tests** checks it (5 tests).

## Summary

- The `Integer` class and `Double` class are **wrapper classes** that create objects from primitive types.
- (AP 4.7.A.1) The `Integer` class and `Double` class are part of the `java.lang` package.
- (AP 4.7.A.1) An `Integer` object is immutable, meaning once an `Integer` object is created, its attributes cannot be changed. A `Double` object is immutable, meaning once a `Double` object is created, its attributes cannot be changed.
- (AP 4.7.A.2) **Autoboxing** is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an `int` to an `Integer` and a `double` to a `Double`. The Java compiler applies autoboxing when a primitive value is:
  > - passed as a parameter to a method that expects an object of the corresponding wrapper class
  > - assigned to a variable of the corresponding wrapper class
- (AP 4.7.A.3) **Unboxing** is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an `Integer` to an `int` and a `Double` to a `double`. The Java compiler applies unboxing when a wrapper class object is:
  > - passed as a parameter to a method that expects a value of the corresponding primitive type
  > - assigned to a variable of the corresponding primitive type
- (AP 4.7.A.4) The following class `Integer` method—including what it does and when it is used—is part of the Java Quick Reference: `static int parseInt(String s)` returns the `String` argument as a signed `int`.
- (AP 4.7.A.5) The following class `Double` method—including what it does and when it is used—is part of the Java Quick Reference: `static double parseDouble(String s)` returns the `String` argument as a signed `double`.
