Variables
Java is a statically typed language — every variable's type is declared and checked at compile time. Java variables come in two families: primitive types (like int, double) that store values directly, and reference types (like String, objects) that store a reference to an object in memory.
💡 Key insight
Unlike C, Java automatically initializes instance variables (fields) to a default value — but local variables inside methods still must be initialized manually.
Syntax
syntax
type variableName = value;Example
The + operator concatenates strings and numbers together when at least one operand is a String.
java
public class Main {
public static void main(String[] args) {
int age = 20;
double price = 49.99;
String name = "CodeyHub";
System.out.println(name + " - " + age + " - " + price);
}
}outputCodeyHub - 20 - 49.99
Try it yourself
Copy this example into the Java compiler and run it — try changing a value and see what happens.
