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.

| Version | Behavior of Underscore (_) | Compiler Response |
|---|---|---|
| Java 7 and earlier | Allowed as a valid identifier (for example, int_=20;). | Compiles successfully without warnings. |
| Java 8 | Still 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. |
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.
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.
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.
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.
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:
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.
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.
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.
We request you to subscribe our newsletter for upcoming updates.