Sub topic 6
Literals and Primitive Types with `var`
Assume you know basic Java variable declarations, primitive types, reference types, and arithmetic expressions. This chapter focuses on how Java chooses a type when a var initializer contains a literal, such as 10, 1.5, or 'A'.
A literal is a value written directly in source code. For example, 10 is an integer literal and "Ada" is a string literal. With var, the compiler infers the variable's static type from the complete initializer expression.
var count = 10;The compiler treats count as an int. It does not treat count as a value whose type can change later:
count = 20; // valid
// count = 20L; // does not compile: long cannot be assigned to intThe type is selected during compilation. There is no runtime type inference or dynamic typing.
Default types for literals
Java gives most numeric literals a default type. Integer literals use int when their value fits. Decimal literals use double unless you add a suffix.
| Literal | Inferred type with `var` | Example |
|---|---|---|
| `42` | `int` | `var n = 42;` |
| `42L` | `long` | `var n = 42L;` |
| `3.14` | `double` | `var n = 3.14;` |
| `3.14f` | `float` | `var n = 3.14f;` |
| `'A'` | `char` | `var letter = 'A';` |
| `true` | `boolean` | `var ready = true;` |
Use L for a long literal and f for a float literal. The suffix is part of the literal's type selection.
public class VarLiterals {
public static void main(String[] args) {
var small = 42; // int
var large = 42L; // long
var precise = 3.14; // double
var compact = 3.14f; // float
var letter = 'A'; // char
var enabled = true; // boolean
var message = "Ready"; // String
System.out.println(small);
System.out.println(large);
System.out.println(precise);
System.out.println(compact);
System.out.println(letter);
System.out.println(enabled);
System.out.println(message);
}
}String is a reference type, not a primitive type. A string literal uses double quotation marks. A char literal uses single quotation marks and contains exactly one character.
Types that need a cast
Java does not infer byte or short from an integer literal assigned through var. The literal 1 has type int, so these declarations require a cast:
var byteValue = (byte) 1; // byte
var shortValue = (short) 1; // shortA cast explicitly asks Java to convert a value to another type. The conversion can lose information when the value is too large for the target type, so use casts carefully.
Expressions can change the result
var infers the type of the entire expression, not necessarily the type of each literal:
var whole = 1 / 2; // int, value 0
var fraction = 1.0 / 2; // double, value 0.5
var wider = 1 + 2L; // long, value 3In 1 / 2, both operands are int, so Java performs integer division before assigning the result. Adding .0 makes one operand a double, which produces floating-point division.
Java also promotes smaller numeric types during arithmetic. For example, adding a byte and a byte produces an int result:
var first = (byte) 10;
var second = (byte) 20;
var total = first + second; // intThe result is int because Java performs the addition using at least int precision.
A quick prediction method
To predict a var type:
- Identify each literal's default type.
- Apply the operators' numeric promotion rules.
- Check for a suffix or cast.
- Assign the resulting expression type to the variable.
Exercise
What are the types and values of var a = 1 / 2;, var b = 1.0 / 2;, and var c = 1 + 2L;?
Show answer
a is an int with value 0, b is a double with value 0.5, and c is a long with value 3.