Java SE 9: “_” (Underscore) changes

Filed Under: Java

In this post, we will discuss about What is the use of Underscore (“_”) in Java Modifiers? Changes to underscore in Java SE 9 release with some simple and useful examples. I have already released many Java SE 9 tutorials. Please go through them to learn all Java SE 9 new features.

Recently Oracle Corp has changed the release date of Java SE 9 from March 2017 to July 2017.

Let us start this post now.

Introduction to Underscore (“_”) in Java

In Java, we can start an identifier with a letter, dollar sign ($) or underscore (“_”). However, Identifiers cannot start with a number.

In Java, we can use underscore in identifiers mainly the following cases:

  • To connect two different words of an identifier.
  • To define constants.
  • To define some lengthy and useful identifier (mostly in unit tests).
  • To define private variables, methods etc.

Before Java SE 8: Underscore (“_”)

Before Java SE 8 release, we can use Underscore in identifiers. And also we can use Underscore alone as an identifier without any issues. Even though it is not recommended, but it works fine without any issues.

Java SE 8: “_” Changes

In Java SE 8 release, using Underscore alone as an identifier is not recommended and it gives a WARNING message. To test it, write a simple Java program in any IDEs like Eclipse and run it.

Example:-


public class Java8UnderscoreTest 
{
  public static void main(String[] args) 
  {
	int _ = 10;
	System.out.println("Value of underscore (_) = " + _);
  }
}

Output:-


Value of underscore (_) = 10

It works fine without any issues. However, we can see the following warning messages in IDEs.


Multiple markers at this line
- '_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on
- Occurrence of '_'

That means Underscore is not allowed as an identifier from Java SE 8 on-wards.

NOTE:-

It is a compile-time error if a Lambda Parameter has the name underscore (“_”).

Java SE 9: “_” Changes

Oracle Corp is going to remove that underscore (“_”) usage as identifier completely in Java SE 9 release.

NOTE:- Disallowing “_” as a legal identifier


jshell> int _ = 10
|  Error:
|  as of release 9, '_' is a keyword, and may not be used as an identifier
|  int _ = 10
|      ^
|  Error:
|  reached end of file while parsing
|  int _ = 10

That’s it all about “Java SE 9: Underscore Changes”. We will discuss some more Java SE 9 New Features in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

Thank you for reading my tutorials.

Happy Java SE 9 Learning!

Comments

  1. Abhinav Mishra says:

    Very nice post…Short and simple…Necessary information provided without any BLA BLA…

  2. Balavenkat says:

    underscore is taken as keyword from java 9 onwards, why?

  3. uday ogra says:

    I have written simple java program which will list down all files in your code base where underscore has been used as an identifier: http://marjavamitjava.com/underscore-keyword-java-9-use-java-code-check-code/

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages