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.
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 22Java 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 errorMultiple 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 variableVariables can also be declared first and initialized later:
String teacherName; // declaration only
int teacherAge; // declaration only
teacherName = "Mr. Wang"; // first assignment
teacherAge = 31; // first assignmentMultiple 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 valueThe 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.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
