Ad space (banner)
โ˜•Java Lessons
Lesson 3 / 68

Working with Variables

This lesson covers the basics of using variables in Java, and understanding safe, explicitly-typed variable declarations. It's for anyone searching "Java variable tutorial."

A variable is like a "labeled box" that holds a value under a name. Declare one with type variableName = value;, and use + to concatenate strings.

The example declares two variables, String name = "Alice" and int age = 14, and uses the + operator to build a greeting by joining strings and variables together. Notice the explicit types, String and int.

A common early mistake is trying to use a variable without declaring its type. This is a key benefit of a statically typed language like Java: declaring a type lets your IDE offer much stronger autocomplete and error detection.

In real projects, giving a variable a name that makes clear "what this value is for" makes code much easier to understand when you come back to read it later.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)