Variables are containers for storing data values. In Java, every variable must be declared with a specific data type, which tells the computer what kind of data the variable will hold.
int
: Stores whole numbers, like 7 or -42.double
: Stores decimal numbers, like 3.14 or -0.01.boolean
: Stores true
or false
values.char
: Stores single characters, like 'A' or 'z'.String
: Stores sequences of characters, like "Hello, Java!".To declare a variable, write the data type, then the variable name, and assign a value with =
:
int age = 16;
double price = 9.99;
boolean isStudent = true;
char grade = 'A';
String name = "Alex";
You can change a variable's value at any time:
age = 17;
price = price - 2.00;
Variables let programs remember information and perform calculations. They're the building blocks for everything in programming!
Imagine variables as labeled jars in a kitchen. Each jar (variable) can hold a specific kind of ingredient (data type).
int
variable.Declaring an int
called score
and setting it to 100.
Using a boolean
named isLoggedIn
to track user login status.
Variables store information; data types define what kind of information can be stored.