Skip to main content Skip to docs navigation
TechSpiderTutorials

Understanding 'const' and 'goto' Keywords in Java

Java is a widely-used, object-oriented programming language known for its robustness, portability, and security features. While it shares similarities with languages like C and C++, it also introduces its own conventions and restrictions. Two notable keywords that differ in Java compared to these languages are const and goto. This article explores why these keywords are not directly supported in Java, alternative approaches used in Java programming, and the principles underlying their absence.

On this page

The Absence of const in Java

In languages like C and C++, the const keyword is used to define constants — variables whose values cannot be changed once assigned. For example:

const int MAX_SIZE = 100;

However, in Java, the const keyword is not used for defining constants. Instead, constants are typically declared using the final keyword. For instance:

final int MAX_SIZE = 100;

The final keyword serves a dual purpose in Java, both for constants and for declaring that a class cannot be subclassed or a method cannot be overridden, depending on where it is used.

The Absence of goto in Java

The goto statement, popular in languages like C and BASIC, allows for unconditional jumps to another part of the code. For example:

int count = 0;
loop:
while (count < 10) {
    count++;
    if (count == 5)
        goto loop; // Jump back to the 'loop' label
}