Sub topic 17
Raw Types and Unchecked Warnings
Generic code is code that uses type parameters, like List<String>. Java still accepts older code written before generics existed. This chapter shows you how to spot that older style and how to understand the warnings the compiler gives you about it.
What Is a Raw Type?
A raw type is a generic class or interface used without its type argument. A type argument is the real type you put inside the angle brackets, such as String in List<String>.
| Code | Kind |
|---|---|
| `List<String> names` | Generic type with a type argument |
| `List names` | Raw type |
| `new ArrayList<>()` | Generic, with the type inferred |
| `new ArrayList()` | Raw type |
Raw types exist for backward compatibility. That means old Java code written before Java 5 still compiles today. With a raw type, the compiler no longer knows what type the elements are, so it cannot check them for you.
What Is an Unchecked Warning?
An unchecked warning is a message from the compiler. It says, "I cannot prove this line is type-safe." The program still compiles, but you have lost the safety that generics give you.
Here is a program that causes two unchecked warnings:
import java.util.ArrayList;
import java.util.List;
public class UncheckedDemo {
public static void main(String[] args) {
List raw = new ArrayList(); // raw type
raw.add("hello"); // unchecked call
List<String> strings = raw; // unchecked conversion
System.out.println(strings.get(0));
}
}By default, javac only prints a short note:
Note: UncheckedDemo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.To see each warning, compile with the -Xlint:unchecked flag:
javac -Xlint:unchecked UncheckedDemo.javaNow the compiler points to the exact lines. One warning is an "unchecked call to add(E) as a member of the raw type List". The other is an "unchecked conversion" from List to List<String>.
How to Fix the Warnings
Most of the time the fix is simple: add the type argument.
import java.util.ArrayList;
import java.util.List;
public class CheckedDemo {
public static void main(String[] args) {
List<String> strings = new ArrayList<>();
strings.add("hello");
System.out.println(strings.get(0));
}
}This version compiles with no warnings. If you write strings.add(42), you now get a compile error instead of a bug at runtime.
Suppressing a Warning
Sometimes you know a line is safe, but the compiler cannot prove it. In that case you can add @SuppressWarnings("unchecked"). An annotation is a tag that starts with @ and gives extra information to the compiler.
Common Mistakes
- Writing
new ArrayList()and leaving out<>. This gives you a raw type. - Ignoring the "uses unchecked or unsafe operations" note.
- Putting
@SuppressWarningson a whole class to make the warnings go away.
Exercise
The line Map scores = new HashMap(); compiles, but a later call to scores.put("Ada", 90) gives an unchecked warning. Why, and how do you fix it?
Show answer
Map is used as a raw type, so the compiler cannot check the key and value types of put. Declare it with type arguments: Map<String, Integer> scores = new HashMap<>();.
Exercise
Which flag makes javac show the details of each unchecked warning?
Show answer
-Xlint:unchecked, for example javac -Xlint:unchecked MyApp.java.