Java Wrapper Types
Create an ArrayList<Integer> named nums, call nums.add(9); then nums.add(4);, and print nums.get(0). Compile Crate.java and run java Crate. The screen should show one line: 9. The angle brackets hold Integer, not int. Storing whole numbers in a list through a wrapper type — so the collection can hold objects — is what this page teaches.
Why the list needs Integer
On the iterating page the list held String words. An ArrayList stores objects. The primitive type int is not an object. The wrapper type Integer is the object form of a whole number. Inside the angle brackets you write Integer. When you add(9), Java can box the primitive 9 into an Integer for you on this page. That quiet boxing is enough for the teaching file.
Open Crate.java. Keep the public class name Crate so the file name matches. Delete leftover for (String t : tags), oak and pine adds, Lane demos, and any HashSet or map leftover. Also delete any Scanner or nextLine. The program must not wait for typing.
Near the top, above public class Crate, write:
import java.util.ArrayList;
Inside main, create the list:
ArrayList<Integer> nums = new ArrayList<Integer>();
Do not write ArrayList<int> — javac refuses that shape. Keep Integer on both sides of the create line for this page. Then add two whole numbers without quotes:
nums.add(9);
nums.add(4);
Then write:
System.out.println(nums.get(0));
Slot zero holds 9. Save before you compile.
import java.util.ArrayList;
public class Crate {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(9);
nums.add(4);
System.out.println(nums.get(0));
}
}
From the folder that holds Crate.java, run javac Crate.java. When parentheses, braces, angle brackets, and semicolons match, javac writes no line of its own. Then run java Crate — not java Crate.java, and not java Integer. Leave the .java ending off the run command.
The program should print one line and stop: 9. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in a file. Delete that call and keep the ArrayList<Integer> create, the two add lines, and System.out.println(nums.get(0));.
Walk the run once. Java starts at Crate.main. The list grows with two boxed whole numbers. The call get(0) returns the first Integer. The print line writes 9. The screen shows 9. Nothing else should print. If you see oak or pine, a Lane iterating demo is still running — switch to java Crate. If you see 4, you printed get(1) by mistake; keep get(0) for the teaching file. If you see [9, 4], you printed the whole list object instead of slot zero — keep println(nums.get(0)) for the quiz path.
If javac rejects ArrayList<int>, change the brackets to Integer. That error is the whole point of this lesson’s type rule, so fix the brackets and compile again. If you write nums.add("9"); with quotes, you are adding a String, and javac refuses for an ArrayList<Integer>. Keep bare digits for this page. If the import is missing, javac cannot find ArrayList — put import java.util.ArrayList; above the class. Spelling Integer with a capital I matters; integer in the brackets will not match the wrapper type Java expects here.
Unbox into a plain int
A second demo reads the first value into a primitive int variable, then prints that variable. Java unboxes the Integer for you on this assignment.
import java.util.ArrayList;
public class Crate {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(9);
nums.add(4);
int n = nums.get(0);
System.out.println(n);
}
}
Compile and run the same way. The screen still shows 9. The line int n = nums.get(0); takes the wrapper value out as a plain int. You do not need to call intValue() on this beginner path. Keep the first teaching file as the direct println(nums.get(0)) shape. Use the unbox file to see that a primitive variable can hold what the list stored as Integer. The name n is yours to keep exactly as shown so the print line stays short. Do not mix a for-each over String tags into this second file — stay with the number list and the one int assignment.
Side by side
One short table keeps the type rule clear.
📊 int vs Integer for lists
| Piece | On this page |
|---|---|
| list type | ArrayList<Integer> |
| illegal | ArrayList<int> — not allowed |
| add | nums.add(9); — boxes to Integer |
| read | nums.get(0) prints 9; may unbox to int n |
The table is only a reminder. The typed pieces you keep are the import, ArrayList<Integer>, the two adds, and get(0). |
Compared with String lists and with primitives alone
On the iterating page, ArrayList<String> held words. That still works. This page switches the element type to Integer so whole numbers can sit in a list. A leftover Lane for-each that prints oak and pine is a different lesson. For this page keep Crate and print 9 from get(0).
A lone int x = 9; still works without any list. Wrappers matter when the collection API asks for an object type in the angle brackets. Other wrappers follow the same idea later — Double beside double, Boolean beside boolean — but this page keeps Integer and int so the screen stays one clear number. You do not need to memorize every wrapper name today; learn the pattern once with Integer beside int and the illegal ArrayList<int> shape.
When old Lane.java files still sit nearby, leave them alone or delete them only if they confuse which class you run. Run java Crate after you compile the wrapper list from this lesson. The Collections path ends here with that clear 9 on the screen before the course turns to errors and files.
What this leaves for exceptions
This page closes the Collections module’s wrapper need for whole numbers in a list. The next module opens errors with exceptions. Stay with ArrayList<Integer> and get(0) here on this page. Do not invent try or catch as a requirement on this page.
Before exceptions
You can import java.util.ArrayList, create ArrayList<Integer> nums, add 9 and 4, and print nums.get(0) to see 9. You can also unbox into int n. Compile Crate.java and run java Crate. The wrapper type let the list hold whole numbers. The next page is exceptions, where a program signals and handles problems.
Keep Integer in the angle brackets. Keep bare number adds without quotes. Keep get(0) for the teaching screen 9. Those three habits are the checkpoint before you move on. When the screen shows 9 from the Integer list after a clean compile of Crate.java, wrappers did their job for this lesson and you are ready for the next topic.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top