π4.7: Wrapper Classes (Integer & Double)
Table of Contents
π This page is a condensed version of CSAwesome Topic 4.7
β΄β΄β΄ NEW UNIT/SECTION! β΄β΄β΄
Create a blank Java program to take your class notes in for the next few lessons.
Click on the collapsed heading below for GitHub instructions ‡
π NOTES PROGRAM SETUP INSTRUCTIONS
- Go to the public template repository for our class: BWL-CS Java Template
- Click the button above the list of files then select
Create a new repository - Specify the repository name:
CS2-Unit4PartB-Notes - For the description, write:
ArrayList data collections, Integer & Double wrapper classes - Click
Now you have your own personal copy of this starter code that you can always access under the
Your repositoriessection of GitHub! π - Now on your repository, click and select the
Codespacestab - Click
Create Codespace on mainand wait for the environment to load, then youβre ready to code! - π Take notes in this Codespace during class, writing code & comments along with the instructor.
π When class ends, donβt forget to SAVE YOUR WORK! Codespaces are TEMPORARY editing environments, so you need to COMMIT changes properly in order to update the main repository for your program.
There are multiple steps to saving in GitHub Codespaces:
- Navigate to the
Source Controlmenu on the LEFT sidebar - Click the button on the LEFT menu
- Type a brief commit message at the top of the file that opens, for example:
updated Main.java - Click the small
βοΈcheckmark in the TOP RIGHT corner - Click the button on the LEFT menu
- Finally you can close your Codespace!
Wrapper Classes: Integer and Double
For every primitive type in Java, there is a built-in object type called a wrapper class. The wrapper class for int is called Integer, and for double it is called Double. Why do this? Sometimes you may need to create a wrapped OBJECT version of a primitive type, so that you can give it to a method that is expecting an object.

π To wrap a value, call the constructor for the wrapper class in earlier versions of Java. In Java 9 on, this is deprecated which means itβs not the best way to do this anymore, and you should instead just set it equal to a value. The AP CSA Exam covers Java 7 which does allow using the constructor.
// in older versions of Java (and on the AP exam)
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 (defined in the java.lang included package) are also useful because they have some special values (like the minimum and maximum values for the type) and methods that you can use.
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.
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
βοΈ Summary
-
The
Integerclass andDoubleclass are wrapper classes that create objects from primitive types. -
(AP 4.7.A.1) The
Integerclass andDoubleclass are part of thejava.langpackage. -
(AP 4.7.A.1) An
Integerobject is immutable, meaning once anIntegerobject is created, its attributes cannot be changed. ADoubleobject is immutable, meaning once aDoubleobject 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
intto anIntegerand adoubleto aDouble. 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
Integerto anintand aDoubleto adouble. 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
Integermethodβincluding what it does and when it is usedβis part of the Java Quick Reference:static int parseInt(String s)returns theStringargument as a signedint. - (AP 4.7.A.5) The following class
Doublemethodβincluding what it does and when it is usedβis part of the Java Quick Reference:static double parseDouble(String s)returns theStringargument as a signeddouble.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.