What is the use of underscore keyword in Java 9?

JavaObject Oriented ProgrammingProgramming

In earlier versions of Java, the underscore ("_") has used as an identifier or to create a variable name. Since Java 9, the underscore character is a reserved keyword and can't be used as an identifier or variable name. If we use a single underscore character as an identifier, the program fails to compile and throws a compile-time error because now it is a keyword and can't be used as a variable name in Java 9 or later versions.

Example

public class UnderscoreKeywordTest {
   public static void main(String args[]) {
      int _ = 50
      System.out.println(_);
   }
}

Output

UnderscoreKeywordTest.java:3: error: as of release 9, '_' is a keyword, and may not be used as an identifier
int _ = 50;
^
UnderscoreKeywordTest.java:4: error: as of release 9, '_' is a keyword, and may not be used as an identifier
System.out.println(_);
raja
Published on 13-Feb-2020 14:59:35
Advertisements