Typed variable
Try it yourself →Variable with a specified type.
Syntax
type name = value;
Description
Java is statically typed: the type of every variable is written. int, double, boolean, String are the most common.
Example
public class Main {
public static void main(String[] args) {
int age = 36;
String name = "Alex";
System.out.println(name + " " + age);
}
}Expected output
Alex 36
When to use?
Storing all kinds of data.
Common mistakes
- ⚠️ Assigning the wrong type causes a compilation error: int x = "5"; → ERROR.