Lesson 17 min read

Variables & Data Types

Give your data a name and a home

What Is a Variable?

Think of a variable like a labeled jar on a shelf. The label is the variable's name, and the stuff inside is the value. In Java, you also have to say what kind of stuff goes in the jar — numbers? letters? true/false? That's the data type.

Java is a statically typed language, which means you pick the jar size before you put anything in it. Once you say "this jar holds integers," you can't suddenly shove a sentence in there.

Declaring & Initializing Variables

public class Main {
public static void main(String[] args) {
int age = 10;
double price = 4.99;
char grade = 'A';
boolean isHappy = true;
String name = "Alex";
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Price: $" + price);
System.out.println("Grade: " + grade);
System.out.println("Happy? " + isHappy);
}
}
Output
Name: Alex
Age: 10
Price: $4.99
Grade: A
Happy? true

The Primitive Types — Java's Building Blocks

Java has 8 primitive types. Think of them as different-sized containers:

  • byte — tiny box (-128 to 127). Good for saving memory when you know values are small.
  • short — small box (-32,768 to 32,767).
  • int — the everyday box (-2 billion to 2 billion). This is your go-to for whole numbers.
  • long — the giant box (up to about 9 quintillion). Add an L at the end: long big = 9999999999L;
  • float — decimal number, less precise. Add an f: float pi = 3.14f;
  • double — decimal number, more precise. This is your default for decimals.
  • char — a single character in single quotes: 'A'
  • boolean — only true or false. Like a light switch.

String is NOT a primitive — it's a class (notice the capital S) that internally uses a character array. But you'll use it constantly, so Java gives it special treatment.

Type Casting — Converting Between Types

public class Main {
public static void main(String[] args) {
// Widening (automatic) — small jar into big jar, no problem
int myInt = 42;
double myDouble = myInt; // int -> double, automatic
System.out.println("int to double: " + myDouble);
// Narrowing (manual) — big jar into small jar, you might lose stuff
double pi = 3.14159;
int rounded = (int) pi; // double -> int, you lose the decimals!
System.out.println("double to int: " + rounded);
// String to int
String numText = "123";
int parsed = Integer.parseInt(numText);
System.out.println("Parsed: " + parsed);
// int to String
String back = String.valueOf(parsed);
System.out.println("Back to String: " + back);
}
}
Output
int to double: 42.0
double to int: 3
Parsed: 123
Back to String: 123

The final Keyword — Constants

public class Main {
public static void main(String[] args) {
final double TAX_RATE = 0.08;
final String SCHOOL = "Riverside Elementary";
double price = 25.00;
double total = price + (price * TAX_RATE);
System.out.println("School: " + SCHOOL);
System.out.println("Total with tax: $" + total);
// TAX_RATE = 0.10; // ERROR! Can't change a final variable
}
}
Output
School: Riverside Elementary
Total with tax: $27.0
Note: Think of final like writing with a permanent marker instead of a pencil. Once you write the value, it's stuck forever. Use it for things that should never change — like the speed of light or your tax rate.

Quick check

Which data type would you use to store someone's exact bank balance like $1042.57?
Operators & Expressions