Using _ (underscore) as Variable Name in Java


In the vast landscape of Java programming, one of the most intriguing aspects is its choice of variable naming. Among these, the use of the underscore (_) is a topic that has sparked much discussion and debate. This article will delve into the usage of underscore as a variable name in Java, exploring its evolution across different Java versions and its current status in the Java programming world.

The Saga of the Underscore in Java

A unique aspect of Java programming lies in its allowance for naming variables. Underscore, or "_", is a valid character that can be used in variable names, but its usage has seen substantial changes in different versions of Java.

Early Java Versions (up to Java 7)

In versions of Java up to Java 7, the use of underscore as a variable name was entirely permissible. Programmers could declare a variable with a single underscore and the code would compile and run without any i

int _ = 10;

Transition Period - Java 8

In Java 8, things began to change. Using an underscore as a variable name became a discouraged practice. In fact, the compiler would issue a warning if you tried to use a single underscore to name your variables. However, it would still compile and run the code. The warning was a sign of things to come in future Java versions.

The Big Change - Java 9 and onwards

Java 9 introduced a major change in this regard. Using a single underscore to name variables was no longer allowed, and trying to do so would cause a compile-time error. This was a drastic shift from the previous versions and was done to free up the underscore for use as a keyword in future versions. However, using multiple underscores (like "" or "_") is still valid.

Why the Change?

One might wonder why such a change was introduced in Java 9. The answer lies in the desire to enhance the language. The aim was to free up the underscore character to use it as a keyword or an identifier with special meaning in future Java versions.

In fact, starting from Java 10, the underscore character has been repurposed and is now used as a keyword to represent unused parameters in lambda expressions. For example:

BiConsumer<Integer, Integer> printer = (a, _) -> System.out.println(a);

In this example, the underscore character is used to denote that the second parameter of the BiConsumer is not used in the lambda expression

Conclusion

The journey of the underscore in Java is a fascinating one. Starting from being a perfectly valid identifier, it transformed into a discouraged practice and eventually became a compile-time error if used as a standalone variable name. This change, introduced in Java 9, was not merely an arbitrary decision but a strategic move to prepare the underscore for special purposes in future versions of the language.

This serves as an important reminder of how languages evolve and how certain elements can be repurposed to facilitate greater expressiveness and efficiency in the code. The underscore in Java is a prime example of this, illustrating how even something as simple as a variable naming convention can undergo significant changes for the betterment of the language.

As a developer, it's essential to stay updated with these changes and understand their reasons and implications. Being aware of the evolution of features in Java, such as the use of underscore as a variable name, allows you to write cleaner, more efficient, and future-proof code. Always keep learning and evolving alongside the language, and continue to enhance your skills and understanding of Java.

Updated on: 19-Jul-2023

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements