Skip to main content
Logo image

Section 11.1 Wrapper Classes - Integer and Double

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.

Subsection 11.1.1 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.
// 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.

Activity 11.1.1.

What’s the minimum and maximum numbers for an int? What happens if you go beyond these limits with - 1 or + 1?
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.

Subsection 11.1.2 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.
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:
Integer i = 2;  // autoboxing - wrap 2
int number = i; // unboxing - back to primitive type

Activity 11.1.2.

Subsection 11.1.3 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:

Activity 11.1.3.

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

Activity 11.1.4.

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

Subsection 11.1.4 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.

Project 11.1.5.

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

Subsection 11.1.5 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.
You have attempted of activities on this page.