Skip to content

Literals and constants (final)

A literal is a fixed value written directly in the source code.

Examples:

int age = 25; // integer literal
double pi = 3.14159; // floating‑point literal
char grade = 'A'; // character literal
String name = "Alice"; // string literal (an object)
boolean flag = true; // boolean literal

Integer literal forms:

int dec = 42; // decimal
int oct = 052; // octal (prefix 0)
int hex = 0x2A; // hexadecimal (prefix 0x)
int bin = 0b101010; // binary (prefix 0b)

Underscores in literals (Java 7+):

int million = 1_000_000;
long creditCard = 1234_5678_9012_3456L;

A constant is a variable whose value cannot be changed after initialization. Declare it with the final keyword.

final double PI = 3.141592653589793;
final int MAX_USERS = 100;

Convention: Constant names are in uppercase with underscores.

Blank final: you can declare a final variable without initializing it immediately, but you must assign it exactly once.

final int SIZE;
SIZE = 10; // allowed only once

Static constants are often used for shared immutable values:

public class MathConstants {
public static final double PI = 3.14159;
public static final double E = 2.71828;
}

They can be accessed as MathConstants.PI without creating an instance.