Home United States USA — software Where To Use the Underscore in Java

Where To Use the Underscore in Java

189
0
SHARE

In this post of our series called “Stranger things in Java”, we will try to clarify the use of the underscore symbol « _ » in various Java programming use cases.
Join the DZone community and get the full member experience. This series of articles called “ Stranger things in Java ”, is inspired by the contents of my book “ Java for Aliens ”. These articles are dedicated to insights into the Java language. Deepening the topics we use every day will allow us to master Java coding even in the strangest scenario. In this article, we will try to clarify the use of the underscore symbol  » _  » in Java programming. So, let’s examine the use cases of this symbol within a Java program. Let’s start with the best-known case. We know that it is possible to use the underscore symbol as part of an identifier of a programming element. In fact, to define an identifier in Java, we can use: However, the standard conventions for defining the identifiers of variables, methods, Java types (classes, interfaces, enumerations, records, and annotations), packages and modules, do not require the use of the underscore symbol. In Java programming, only the standard convention for constant identifiers requires the use of the underscore symbol. In fact, this convention recommends defining names consisting of uppercase characters only and using the underscore symbol as a word separator to improve readability. For instance: This is because a constant must be easily recognizable, and by using all capital characters the developer is able to distinguish the constants from the variables at a glance. Up to version 8 of Java, it was even possible to use just one underscore symbol as the identifier of a Java programming element, such as a variable or a method. In version 8, however, the compiler already issued a warning in order to not use this practice. In fact, if we tried to define for example a variable like this with Java 8: The code compiled correctly, but this warning was printed: Which warned us that the use of the underscore symbol as an identifier may no longer be supported in future versions.

Continue reading...