Skip to content
Codewilds

Lesson 2 of 34 · Fundamentals

Java Variables and Primitive Data Types

easy10 XP3 min readFree lesson

A program needs to remember things: a player's name, a score, whether a door is locked. In Java you store values in variables — and every variable has a type that says exactly what kind of value it can hold.

Declaring a variable

A variable declaration has three parts: the type, the name and (usually) a starting value.

int level = 7;          // a whole number
String heroName = "Aria";
level = level + 1;      // assign a new value: level is now 8

Java is statically typed: the type is fixed when you declare the variable and checked by the compiler. You can change the value of level as often as you like, but it can only ever hold int values. Assigning level = "high"; is a compile error. That strictness catches whole classes of bugs before your program even runs.

The 8 primitive types

Java has eight built-in primitive types for simple values:

Type Holds Example
int whole numbers (about ±2.1 billion) int gold = 1250;
long very large whole numbers long stars = 9_000_000_000L;
double decimal numbers double health = 92.5;
boolean true or false boolean hasMap = true;
char a single character, in single quotes char rank = 'S';
byte, short, float smaller/rarer variants float speed = 1.5f;

In everyday code you will mostly use int, double, boolean, char and long. Underscores in number literals (9_000_000_000L) are allowed for readability, and a long literal that does not fit in an int needs the L suffix.

String is not on the list: it is a class, not a primitive — which is why it starts with a capital letter. Strings use double quotes ("Aria"), chars use single quotes ('S').

Printing variables

Use + to join text and values into one String:

String heroName = "Aria";
int level = 7;
System.out.println("Hero: " + heroName + ", level " + level);
Hero: Aria, level 7

var and final

Since Java 10 you can let the compiler infer the type of a local variable with var. The variable is still strictly typed — the type just comes from the value:

var gold = 1250;        // gold is an int
var title = "Ranger";   // title is a String

Mark a variable final when its value must never change. Constants are written in UPPER_SNAKE_CASE by convention:

final int MAX_LEVEL = 99;
// MAX_LEVEL = 100;     // compile error: cannot assign a value to final variable

Ordinary variable names use camelCase: heroName, hasMap, maxHealth.

Common mistakes

  • Text glued to math: "Next level: " + level + 1 prints Next level: 71. Java reads left to right: "Next level: " + level is already a String, and + 1 then appends the digit 1. Write "Next level: " + (level + 1) instead.
  • Decimals in an int: int price = 9.99; fails with possible lossy conversion from double to int.
  • Using a variable before giving it a value: a declared-but-unassigned local variable cannot be read (variable might not have been initialized).
  • Wrong quotes: String name = 'Aria'; does not compile; '...' is only for a single char.

Assignment

Complete the program so that it stores a hero's stats in variables and prints exactly:

Hero: Aria
Level: 7
Health: 92.5
Gold: 1250
Has map: true
Rank: S
Next level: 8
  1. Declare the variables level (int, 7), health (double, 92.5), gold (int, 1250), hasMap (boolean, true) and rank (char, 'S'). heroName is already there.
  2. Print one line per stat as shown, using the variables (not hard-coded text).
  3. The last line must be calculated from level — the hero's next level.

Variables and data types in other languages

Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.

Lesson last updated on .

Write code ↓
Java+10 XP

Press Run to execute your code, or Submit to check it against the tests.