Coding/Learn/java/variables
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnVariables & Declaration
5 / 31 0%
Lesson 05 of 31
6 mins read

Variables & Declaration

PreviousNext Lesson
In Plain English

A variable is a named storage container in memory. Because Java is statically typed, you must specify the exact data type before the variable name. Once declared, Java guarantees that container will only hold values matching that type.

The Mental Model

"Variables are like labeled storage bins in a workshop. A bin labeled "Screws" (type int) won't let you shove a pair of shoes (String) into it."

Deep Dive: How It Works

Declaration syntax: `type variableName = initialValue;`

Java variables must be declared before they can be used or referenced.

You can declare a variable first and assign its value later, or declare and initialize in one line.

The `final` keyword marks a variable as a constant (its value cannot be modified after assignment).

Syntax Blueprint

dataType variableName = value;
final dataType CONSTANT_NAME = value;

Specify the type (e.g. int, double, String), give it a camelCase name, and assign with =.

PointCore Rules to Remember

Point star
Explicit Typing: Java requires type declarations (e.g., int score = 100;).
Point star
Reassignment: Update a variable without repeating the type: score = 200;
Point star
Constants with final: final double PI = 3.14159; prevents accidental changes.

Common Beginner Traps & How to Fix Them

Mistake:Re-declaring type on update (int score = 10; int score = 20;)

Why it happens: Specifying the type twice in the same scope causes "variable score is already defined".

How to fix: Drop the type keyword on reassignment: score = 20;

Pro Tip: Use descriptive variable names like userAge or currentBalance rather than single letters like x or a.

Live Interactive Example

Hit Run Code to see it live
Declaring and Updating Variables
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output Console
Click "Run Code" to view the rendered output.
How it works: playerLevel is updated from 1 to 2, while MAX_LIVES remains fixed as a constant.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Declare a student score variable

  • PointDeclare an int variable named "score" with the value 95.
  • PointPrint "Student Score: " + score.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates