← Java from Scratch
Lesson 4 of 88 · 10 XP
Variables
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
int lives = 3;
double speed = 2.5;
String name = "Kai"; // String has a capital letter: it's a class
boolean alive = true;
final int MAX_LIVES = 5; // final — a constant, it can't be changed
var score = 100; // Java 10+: the compiler works out the type (int)
int x = 1, y = 2, z = 3; // several at once
- Java has strict static typing: a variable's type is known at compile time and doesn't change;
- a local variable must be initialised before it's read — otherwise you get the error
variable might not have been initialized; - constants are usually written in
UPPER_SNAKE_CASE.
Task. Create name = "Alex", level = 7, hp = 92.5 and print Alex — level 7, HP 92.5.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.