Java Tutorial: Variables
Declare variables in Java — types, assignment, naming rules, var, and the difference between primitives and references.
Give your data a name
Java variables are typed — you declare the type AND the name at the same time. This catches entire categories of bugs before the program even runs.
`int age = 25;` — age is an integer, always. You can't accidentally put a string in it later.
What you'll learn in this Java variables tutorial
This interactive Java tutorial has 8 hands-on exercises. Estimated time: 18 minutes.
- Declare your first variable — In Java, you declare a variable by specifying its **type** then its **name**:
- Create your own variables — Create four variables:
- Reassigning variables — Declare a variable once with its type. After that, just use the name — no type keyword needed:
- final — Java's constant — `final` variables cannot be reassigned once set — they are constants:
- Increment and decrement — Adding or subtracting 1 is so common Java has shortcuts:
- var — type inference (Java 10+) — Since Java 10, you can use `var` to let Java infer the type from the value:
- Naming rules and conventions — Java variable naming rules:
- Build a profile — No starter code. Write everything from scratch.
Java Variables concepts covered
- Give your data a name