Skip to content
AgileLogics
5 min read

Variables & Data Types

Prerequisites: How Java code runs

Every Java variable has a fixed, declared type. The primitive types (int, double, boolean, char, plus long, short, byte, float) hold raw values directly; everything else (including String) is an object reference.

Syntax

int age = 36;
double price = 19.99;
boolean isActive = true;
char grade = 'A';
String name = "Ada";

Example

int score = 10;
System.out.println(score);
score = 20;
System.out.println(score);

Expected output

10
20

Use final for a variable that should never be reassigned, similar to const in JavaScript:

final int MAX_SCORE = 100;

Common mistakes

  • Trying to assign a value of the wrong type without conversion, e.g. int x = "5"; — this is a compile error, not something Java quietly converts.
  • Forgetting that char uses single quotes ('A') while String uses double quotes ("A") — they are different types.

Practice

Declare a final variable called cityName holding your city's name, then print it.

Show solution
final String cityName = "Lagos";
System.out.println(cityName);