Java 9 Underscore Changes | Java 9 Underscore Keyword

Last Updated : 27 Feb 2026

In Java 9, the underscore (_) is reserved as a keyword. It means, we cannot use the underscore as an identifier. There are always improvements with every new version of Java. One of the small changes happened in Java 9 which is related to the underscore (_) symbol. It may look like a short change but it affects the working of the code.

Java 9 Underscore Changes | Java 9 Underscore Keyword

Evolution of Underscore in Java

VersionBehavior of Underscore (_)Compiler Response
Java 7 and earlierAllowed as a valid identifier (for example, int_=20;).Compiles successfully without warnings.
Java 8Still allowed, but discouraged.Compiles with a warning: "'_' used as an identifier might not be supported in releases after Java SE 8".
Java 9 and Later Versions_ becomes a reserved keyword.Compilation fails with an error if _ is used as a variable name.

Why these changes were necessary?

  • The underscore was reserved in Java 9 to avoid confusion and future conflicts.
  • It enforces better coding practices by preventing meaningless variable names.
  • The warning in Java 8 was a transitional step to prepare developers for the stricter rule in Java 9.

Usage of Underscore (_) in Java 7

We can use the underscore (_) as a normal identifier, meaning the underscore can be used as a method name or variable name. We do not get any warning or compile-time error.

Example

Output:

Value of underscore variable: 25
Sum is: 35

Explanation

We have created two variables, one having a value of 2 and another having a value of 10 and their sum is calculated in the variable named sum. The single underscore (_) is used as a variable name. And then both values are printed on the screen. We have performed basic addition to check how the single underscore is used in Java 7.

Note: The above program successfully compiles on Java 7 or older versions only. If you will execute it on Java 8 or later versions, it will give compilation error "underscore not allowed here".

After Java 8 Using Underscore (_)

Using a single underscore (_) in Java 8 throws a warning. It became a compile-time error since Java 9 which is termed as a reserved keyword. It is done to make Java ready for its future features, where developers can use appropriate variable names. To improve the readability, we can use it within the variable names or in the numeric literals.

Example

Output:

Value: 25
Sum: 35
Big Number: 1000000

Explanation

Here, we have not used an underscore as a variable name instead, we use meaningful names such as value and number. Firstly, it calculates the sum and difference of these two variables and then prints the results. Also, we have shown the underscores which can still be used inside the number to make it easy to read such as 1_000_000 and 45_500.

And the underscore is also allowed inside the variable names so that the names such as total_score and highest_score are also valid. The program then prints all of the values and shows the math operations and uses the underscores in the variable and numeric values. We write the readable code while following the Java 8 rules for the underscores.

After Java 9 Underscore (_)

From using Java 9 and onward, underscore (_) is reserved completely and cannot be used as a variable name, identifier or method name. If we ever try to use (_) alone in the code then the compiler shows compile-time error. It is made to prepare Java for the future language features and to avoid confusion.

We can use the single underscore even though it is not allowed such as:

  • Inside variable names( total_score or highest_score).
  • Inside numbers to improve the readability of code (1000_000).

In short, after Java 9 _ is no longer allowed by itself and the developers should use meaningful names for the variables, while the underscore is fine inside the names or numbers.

Example

Output:

First Value: 50
Second Value: 20
Sum: 70
Difference: 30
Large Number: 2000000
Total Marks: 85
Highest Marks: 100

Explanation

The program shows the correct use of variables and underscores. The firstValue and SecondValue are created and values are assigned then their sum and differences are calculated and their results are printed on the output screen. We have also used underscores inside numbers such as 2_000_000 so that we can easily read the large number of values. We can also use underscore in the variable names such as total_marks and highest_marks but we cannot use the single underscore (_) as a variable name in Java 9 and in the newer versions of Java 9.

Advantages

  • Reduces Confusion: When programmers use names for variables that are meaningful then it helps to understand the code more clearly such as instead of some random letters, we can use letters such as userAge or totalPrice.
  • Improves Code Readability: The developers can use the descriptive name and get better readability by using the approach.
  • Better Code Maintenance: As we use better variable names, we can easily maintain and fix bugs in the code.
  • Reduces Confusion: It does not describe a single underscore clearly. Hence, using proper names makes the purpose of variables clear.
  • Encourages Good Coding Practice: It encourages writing clean and professional code with the proper naming conventions.

Disadvantages

  • Extra Work for Developers: If underscore is used in a large project as an identifier then the developers need to rename those variables which takes time and effort.
  • Compatibility Issues: Some of the older libraries or examples found online are still in use _ as a variable name which can create errors when copied into the newer versions.
  • Small Change, Big Impact: It stops a program from compiling completely if not corrected but it seems like a minor rule change.
  • Learning Adjustment: We get confused while learning with the older version because the underscore is allowed for variable names.

Conclusion

From Java 9 onward, _ is no longer usable as a variable name or identifier. Developers should use meaningful names instead, aligning with clean coding principles.