Skip to content
Related Articles

Related Articles

Using _ (underscore) as Variable Name in Java

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 25 Nov, 2021

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”.

Example:

Java




// Java Program to Illustrate Usage of Underscore
// As a Variable Name
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Declaring underscore as variable
        // in java 8 only
        int _ = 10;
  
        // Printing the value stored in it
        System.out.println(_);
    }
}


Output: 

10

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

Example:

Java




// Java program to illustrate Usage of Underscore
// As Variable Name in Java9
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
  
        // Declaring underscore as variable
        // in java9 and onwards
        int _ = 10;
  
        // Printing the value as stored in underscore
        System.out.println(_);
    }
}


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: 

  1. Using underscore in a variable like first_name is still valid. But using _ alone as a variable name is no more valid.
  2. 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 review-team@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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!