Loading...

Java Generics

Name the element type inside angle brackets when you build a list. In Bin.java, keep the public class Bin, import java.util.ArrayList, create ArrayList<String> names = new ArrayList<>();, add "oak", and print names.get(0). Compile with javac Bin.java and run java Bin. The screen should show oak. A second demo stores an Integer value 3 the same way. Leave optional Lambda for the next page. This lesson is typed lists with generics, not command-line args and not a Scanner mini project.

A String list with a type argument

Open Bin.java. Keep the public class name Bin. Delete leftover Flag program-arguments demos, leftover Box Scanner prompts, leftover Build compiling prints, and leftover Shelf collection history if they bury the generics story. The program must not wait for typing and must not read args.
Add the import at the top:
import java.util.ArrayList;
Inside Bin.main, create a list whose elements are String:
ArrayList<String> names = new ArrayList<>();
The angle brackets after ArrayList hold the type argument String. The empty <> on the new side is the diamond — Java fills the same type from the left. Then store one word and print it by index:
names.add("oak");
System.out.println(names.get(0));
get(0) returns a String because the list was declared with String. The screen shows oak.

Bin ArrayList String prints oak

java
import java.util.ArrayList;

public class Bin {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("oak");
        System.out.println(names.get(0));
    }
}

From the folder that holds Bin.java, run javac Bin.java. When braces, brackets, and semicolons match, javac writes no line of its own. Then run java Bin — not java Bin.java. Keep one public class so the name matches Bin.
Walk the run once. Java starts at Bin.main. The list is typed for String. add stores "oak". get(0) returns that word. println writes oak. Nothing prints args[0] from Flag. Nothing waits at item:. Nothing prints [oak, pine] unless you still run an old Shelf file.
Say the beginner rule while you watch the screen: angle brackets name the element type; diamond repeats it on new; get returns that type. Do not invent Lambda demos on this page. Lambda waits for the next optional lesson. Do not leave the list raw as ArrayList names without a type argument in this teaching file.
If you still see oak from java Flag oak, that was program arguments. Here oak is a list element, and the run command is only java Bin. If javac cannot find ArrayList, check the import line.

An Integer list with the same habit

Generics are not only for words. Keep the same public class Bin. Build a list of Integer values:
ArrayList<Integer> nums = new ArrayList<>();
nums.add(3);
System.out.println(nums.get(0));
Compile again with javac Bin.java and run java Bin. The screen shows 3. The type argument is Integer because ArrayList stores objects, and Integer is the object form for whole numbers in this beginner shape. The diamond still copies the type from the left side.

Bin ArrayList Integer prints 3

java
import java.util.ArrayList;

public class Bin {
    public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<>();
        nums.add(3);
        System.out.println(nums.get(0));
    }
}

Walk the run once. The list is typed for Integer. add(3) stores the value. get(0) returns it. println writes 3. Keep the two demos as two teaching shapes. First prove a String list. Then prove an Integer list. Both shapes share import, diamond, add, and get(0).
If you write ArrayList<String> and then add(3), javac rejects the mismatch. If you write ArrayList<Integer> and then add("oak"), javac rejects that mismatch too. The type argument is a contract for what the list holds.

Typed list habits

Keep the type argument and the diamond together while you practice.

📊 Generics habits for Bin

Habit Typed shape Beginner check
Declare ArrayList<String> names angle brackets name the element type
Create new ArrayList<>() diamond copies the type from the left
Store String names.add("oak") value matches String
Store Integer nums.add(3) value matches Integer
Read get(0) returns the declared element type

Generics is not Flag and not Lambda

Program Arguments filled String[] args from the launch line. Generics names the element type of a collection you build in code. Lambda will shorten some call shapes on a later optional page. Do not mix those three jobs in one beginner file today.
oak in Bin is a stored list value. oak after java Flag was an argument. Same spelling, different job. When a leftover Flag file still needs words on the command line, leave it as history and run java Bin with no extra words.
Without a type argument, a raw list loses the clear contract this page teaches. Keep ArrayList<String> or ArrayList<Integer> in the demos you keep. Keep Lambda syntax off this page until that lesson.

Folder checks before Lambda

List the folder after a successful compile. You should see Bin.java and Bin.class together. Edit the source, save, run javac Bin.java, then run java Bin. A stale .class can show an old print if you skip compile after an edit.
If javac complains about duplicate public classes, keep only Bin public. If the screen shows [oak] instead of oak, you printed the whole list instead of get(0) — keep the teaching print as get(0). If nothing prints, confirm main still calls println.
Do not paste Flag argument loops into Bin. Do not paste Box Scanner prompts into Bin. The optional generics job is typed lists with a clear element type.

Before optional Lambda

You can keep public class Bin, import ArrayList, build ArrayList<String> with a diamond, add "oak", print get(0) as oak, then build ArrayList<Integer>, add 3, and print 3. Compile with javac Bin.java and run java Bin. The table separates declare, create, store, and read. The next optional page is Lambda.
Keep the Bin demos for a clear generics pair. Keep Flag arguments files off this page when you practice typed lists. Keep Box Mini Project files off this page when you practice typed lists. Keep Lambda work off this page until that lesson. Those habits are the checkpoint before you move on. When the String demo shows oak or the Integer demo shows 3, Generics did its job for this lesson and you are ready for Lambda if you continue the optional path.
One last pass through the folder: one public Bin, import present, type arguments in angle brackets, diamond on new, matching add values, get(0) print, no Scanner wait, no launch words required. Run javac Bin.java and java Bin once more on the demo you keep open. If the screen still shows oak or 3, the Generics shape is solid and the course can move to optional Lambda on the next page. Prefer one clear typed-list habit per read so each pass teaches the same map.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 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