Using _ (underscore) as variable name in Java
Java 9 has made changes in features of java language and eliminating underscore from legal name is a major change made by Oracle.
- The use of the variable name _ in any context is never encouraged.
- Latest versions of the Java reserve this name as a keyword and/or give it special semantics. If you use the underscore character (“_”) an identifier, your source code can no longer be compiled. You will get compile time error.
Using underscore as 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”. (See JDK-8005852 Treatment of ‘_’ as identifier)
// Java program to illustrate // using underscore as // variable name class UnderScore_works { public static void main(String args[]) { int _ = 10; System.out.println(_); } } |
Output:
10
Using underscore as variable name in Java 9
In Java 9, underscore as variable name won’t work altogether. Below source code can no longer be compiled.
// Java program to illustrate // using underscore as // variable name in java 9 class UnderScore_dont_works { public static void main(String args[]) { int _ = 10; System.out.println(_); } } |
Important points:
- Using underscore in a variable like first_name is still valid. But using _ alone as variable name is no more valid.
- Even if you are using earlier versions of Java, using only underscore as variable name is just plain bad style of programming and must be avoided.
Related article : Variables in Java
This article is contributed by Abhishek Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Recommended Posts:
- Java.util.BitSet class methods in Java with Examples | Set 2
- Shadowing of static functions in Java
- How does default virtual behavior differ in C++ and Java ?
- How are Java objects stored in memory?
- How are parameters passed in Java?
- Are static local variables allowed in Java?
- final variables in Java
- Default constructor in Java
- Assigning values to static final variables in Java
- Comparison of Exception Handling in C++ and Java
- Does Java support goto?
- Arrays in Java
- Inheritance and constructors in Java
- More restrictive access to a derived class method in Java
- Comparison of static keyword in C++ and Java



