Sub topic 1
Starting Point: Explicit Types in Java
Java is a statically typed language. A statically typed language checks the type of each variable and expression at compile time, before the program runs. A variable has a fixed static type, so later assignments and method calls must agree with that type.
This chapter establishes the model that var builds on. The next chapters will show how the compiler can infer a local variable's type without making Java dynamically typed.
Prerequisites
You should know how to:
- Declare and initialize local variables.
- Distinguish primitive types, such as
int, from reference types, such asString. - Read basic generic types, such as
List<String>. - Use inheritance and interfaces at a basic level.
- Compile and run a small Java program.
Explicit local variable declarations
An explicit declaration writes the variable's type in the source code:
public class ExplicitTypes {
public static void main(String[] args) {
int count = 10;
String name = "Ada";
boolean active = true;
System.out.println(name + " has " + count + " messages.");
System.out.println("Active: " + active);
}
}Each declaration contains three important parts:
- The type, such as
intorString. - The variable name, such as
countorname. - An optional initializer, which supplies the first value.
The compiler uses the declared type to check every later use. For example, count can hold an int, and integer operations are valid on it. Calling a String method on count would be rejected.
int count = 10;
// count = "ten"; // Compile-time error
// count.toUpperCase(); // Compile-time errorA compile-time error prevents the program from compiling. This check is one reason Java can catch many mistakes before execution.
Declared type and object type
For a reference variable, the declared type and the object's actual class can differ. The declared type controls which operations the compiler allows. The object type controls which overridden implementation runs at runtime.
import java.util.ArrayList;
import java.util.List;
public class ReferenceTypes {
public static void main(String[] args) {
List<String> words = new ArrayList<>();
words.add("java");
System.out.println(words.get(0));
}
}Here, words has the declared type List<String>, but it refers to an ArrayList<String> object. Code using words can call methods declared by List, but not methods that exist only on ArrayList.
| Variable declaration | Declared type | Actual object type | Operations visible through the variable |
|---|---|---|---|
| `List<String> words = new ArrayList<>();` | `List<String>` | `ArrayList<String>` | Methods in `List<String>` |
This distinction is called polymorphism. It lets code depend on a general interface instead of one specific implementation.
Why this matters for `var`
Local variable type inference changes how the type is written, not the type system itself. Inference means that the compiler determines a type from available information. With var, the initializer provides that information.
For example, these declarations are conceptually related:
ArrayList<String> names = new ArrayList<String>();
var otherNames = new ArrayList<String>();In the second declaration, the compiler infers ArrayList<String>. The variable still has a fixed static type, and the compiler still checks method calls and assignments. It does not become an untyped container that can hold unrelated values.
The choice between an explicit type and var affects how much information the source code states directly. An explicit type can emphasize an abstraction such as List<String>. An inferred type can reduce repetition when the initializer already makes the type obvious.
The following chapters use this model to examine exactly what the compiler infers, where inference is allowed, and when writing the type explicitly communicates intent more clearly.
Exercise
In List<String> items = new ArrayList<>();, what is the declared type, and what is the actual object type?
Show answer
The declared type is List<String>. The actual object type is ArrayList<String>.