Using _ (underscore) as Variable Name in Java
As we do know variables in java or rather in any language is introduced to write a code where it is suggested to give meaningful names to the variables as per their usage in code and especially in object-oriented languages are supposed to be used locally where ever it is possible instead of just using them globally. It is a very essential property of variables that is been checked is enterprising domain as it helps us achieve cleaner and minimalistic code.
Case 1: Using underscore as a variable name in Java 8
Although it is supported in Java 8, a mandatory warning is issued if you use _ as an identifier, telling you that “use of ‘_’ as an identifier might not be supported in releases after Java SE 8”.
With the advancement in the version of Java, Java 9 has made changes in features of the java language, and eliminating underscore from the legal name is a major change made by Oracle. The use of the variable name _ in any context is never encouraged. The latest versions of Java reserve this name as a keyword and/or give it special semantics. If you use the underscore character (“_”) as an identifier, your source code can no longer be compiled. We will get a compile-time error.
Case 2: Using underscore as a variable name in Java 9
Output: In Java 9, underscore as variable name won’t work altogether. Below source code can no longer be compiled.
Below are the following conclusions been drawn from the above examples as illustrated:
- Using underscore in a variable like first_name is still valid. But using _ alone as a variable name is no more valid.
- Even if you are using earlier versions of Java, using only underscore as a variable name is just a plain bad style of programming and must be avoided.
This article is contributed by Abhishek Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.