Fundamentals 7 min read

Java Basics: Variables and Constants Explained from Scratch

This note introduces Java constants and variables, covering their definitions, naming conventions, declaration syntax, initialization rules, multiple declarations, and how constants differ from mutable variables through concrete code examples.

Lisa Notes
Lisa Notes
Lisa Notes
Java Basics: Variables and Constants Explained from Scratch

Constants in Java

In Java, data whose value cannot change during program execution is called a constant. Java distinguishes literal constants (fixed values written directly in code such as 345, 34.56, Math.PI) and character constants (identifiers representing fixed values that must be declared before use).

Constants are declared with the final keyword. The general form is:

final ConstantType CONSTANT_NAME = ConstantValue;

Examples:

final float NUM = 2.141F; // declare a float constant and initialize to 2.141
final int INT_NUM = 22; // declare an int constant and initialize to 22

Java naming rules require constant identifiers to be uppercase; if the name consists of multiple words, underscores separate them (e.g., INT_NUM).

Constants can also be declared without immediate initialization and assigned later, but once a value is set it cannot be changed:

final float FLOAT_NUM; // declaration only
FLOAT_NUM = 2.153F; // first (and only) assignment
// Attempting to reassign later causes a compilation error

Multiple constants of the same type can be declared in one statement:

final float A_FLOAT, B_FLOAT, C_FLOAT;
final float A_FLOAT = 2.54F, B_FLOAT = -32.76F, C_FLOAT = 46.32F;

Variables in Java

A variable holds data that can change while the program runs. Declaring a variable requires specifying its type and name, following Java’s identifier naming rules: the first word starts with a lowercase letter, and subsequent words use camelCase.

Variable declaration syntax: VariableType variableName; Examples of declaration and immediate initialization:

String teacherName = "Mr. Wang"; // declare and initialize a String variable
int teacherAge = 31; // declare and initialize an int variable

Variables can also be declared first and initialized later:

String teacherName; // declaration only
int teacherAge; // declaration only
teacherName = "Mr. Wang"; // first assignment
teacherAge = 31; // first assignment

Multiple variables of the same type can be declared together:

String teacherName, teacherHobby;
String teacherName = "Mr. Wang", teacherHobby = "Teaching";

Unlike constants, a variable’s value can be reassigned at any point in the program, as shown by changing teacherName from "Mr. Wang" to "Ms. Zhang".

teacherName = "Ms. Zhang"; // reassign variable value

The note emphasizes that understanding these basic syntactic forms is essential for writing correct Java programs that can run on any operating system, reflecting Java’s “write once, run anywhere” philosophy.

JavaSyntaxVariablesprogramming basicsConstants
Lisa Notes
Written by

Lisa Notes

Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.