Java 9 Underscore(_) keyword

In this post, we will see about Underscore keyword, which is introduced in java 9.

Underscore In Java

Underscore(_) is a symbol that is used to combine multi-words in a single identifier sometimes refers to a variable in a programming context.

In Java, to create a lengthy variable, we prefer to use underscore (_) such as employee_id, employee_name etc.

Apart from it, underscore can be used to represent a variable and can hold any type of value . Since Java 8, we could use underscore as variable name such as = 10, = 10.5, etc. But in Java 9, underscore is a reserve word and cannot be used as an identifier.
Let’s see an example.

Underscore as variable in Java 8

This example is executed by using Java 8 and produce output with a warning message. See the output.

Output

10
warning: ‘_’ used as an identifier
(use of ‘_’ as an identifier might not be supported in releases after Java SE 8)

Underscore as variable in Java 9

If we execute this example with Java 9 then it reports an error. See the example.

Output

error: as of release 9, ‘_’ is a keyword, and may not be used as an identifier

Although, we can not use underscore alone as a variable name, but we can use it with other alphabets. See some possible examples.

Here is an example:

Output
10
20
20
Error: ‘_’ should not be used as an identifier, since it is a reserved keyword from source level 1.8 on

That’s all about Java 9 underscore keyword.

Was this post helpful?

Related Posts

  • 12 October

    Java 9 JShell tutorial

    Table of ContentsHello World Example Using JShellWhat can we do with JShell?How to start and stop JShell?Creating variable in JShellJShell as a CalculatorCreating Methods in JShellCreating Class in JShellExecuting Algebraic Expressions in JShellExecuting Conditional Statements in JShellJShell CommandsList the VariablesList the MethodsImport Java PackagesCheck history of commandsEdit code in JShell Edit PadLoad external Java code […]

  • 11 October

    Java 9: Stream API Improvements

    Table of ContentsThe takeWhile() MethodExampleThe dropWhile() MethodExampleThe ofNullable() MethodExampleThe iterate() MethodExampleWas this post helpful? In Java 9, following methods are added to Stream interface of stream package. These methods are default and static. The takeWhile() Method This method is included in Stream interface and used to collect the elements into a stream. It takes all […]

  • 24 September

    Java 9 – Process API Improvements

    Table of ContentsThe Process ClassStart a new Process?MethodsExample to get Process IdExample to get Process InformationProcessHandle InterfaceMethodsExampleProcessHandle.Info Interface MethodsWas this post helpful? In this post, we will see about Java 9 process API improvements. Java improved its Process API in Java 9 version that includes new methods for Process class and two new interfaces ProcessHandle […]

  • 19 September

    Java try with resources

    Table of ContentsOlder approach to close the resourcesJava 7 try with resourcesSyntaxExampleJava 9 Try with resources ImprovementsFinally block with try with resourcesCreate Custom AutoCloseable CodeWas this post helpful? In this post, we will see about Java try with resources Statement. Java try with resources is a feature of Java which was added into Java 7. […]

  • 19 September

    Java 9 – Javadoc Improvement

    Table of ContentsWhat is JavadocSimplified Doclet APISupport for HTML5Improved SearchSupport for Module SystemHow to create HTML Javadoc from Java source file.Run Javadoc CommandWas this post helpful? Java has upgraded its Javadoc for better user interaction and user-experience. It includes a simplified search, HTML generator, Doclet API, etc. What is Javadoc The Javadoc is a tool […]

  • 19 September

    Java 9 – @SafeVarargs Annotation

    Table of [email protected] [email protected] with Final [email protected] with Static MethodJava 9 @SafeVarargs ImprovementWithout @SafeVarargs annotationWas this post helpful? In this post, we will see about @SafeVarargs Annotation in Java 9. The @SafeVarargs is an annotation that is used to perform safe operations. When a method takes variable arguments, then it may cause to unsafe operation, […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.