Declaring variables
Description
Declaration gives a type and a name.
Explanation
Initialization assigns the first value.
Example
int score = 76;← Chapter 2 — Core Syntax & Data
mixed
A variable is a named location that holds a value which may change. Declare its type before using it so Java knows what kind of data it should store.
A constant uses final and receives a value once. Constants make important fixed values, such as a passing score, visible and safer to reuse.
Declaration gives a type and a name.
Initialization assigns the first value.
int score = 76;A final variable cannot be reassigned.
By convention, named constants use uppercase letters.
final int PASSING_SCORE = 40;Java requires local variables to be assigned before they are read. This protects you from accidentally using an unknown value.
public class VariableDemo {
public static void main(String[] args) {
final int PASSING_SCORE = 40;
int score = 76;
System.out.println(score >= PASSING_SCORE);
}
}