110

I've just replaced s in the following lambda expression by _:

s -> Integer.parseInt(s)

Eclipse compiler says:

'_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on.

I haven't found any explanation in the JLS §3.9 Lexical Structure / Keywords.

Tunaki
138k46 gold badges372 silver badges446 bronze badges
asked May 7, 2014 at 17:02

4 Answers 4

95

The place to look is JLS §15.27.1. Lambda Parameters

It is a compile-time error if a lambda parameter has the name _ (that is, a single underscore character).

The use of the variable name _ in any context is discouraged. Future versions of the Java programming language may reserve this name as a keyword and/or give it special semantics.

So the Eclipse message is misleading, especially as the same message is used for both cases, when an error is generated for a lambda parameter or when a warning is generated for any other _ identifier.

answered May 7, 2014 at 18:25
Sign up to request clarification or add additional context in comments.

12 Comments

Note that as of Java 9, _ will be disallowed as any legal identifier names and not only as a lambda parameter name. This was actually fixed in build 43 : bugs.openjdk.java.net/browse/JDK-8061549
@lscoughlin: Isn’t "Future versions of the Java programming language may reserve this name as a keyword and/or give it special semantics" statement enough? Well, replace "may reserve" by "will use", and you’ll get the picture. Maybe this mail reference helps...
What is this? Java breaking backwards compatibility?
@Arturo Torres Sánchez: that’s nothing new. There were times when enum and assert were legal identifiers...
@Holger actually there are tons of languages that use underscore as a name placeholder (Scala, Clojure, F#, SML, Erlang, just to name a few). It's an established pattern that traces back to 90s or 80s, I believe, so disobeying it is odd.
|
38

It is the Phase 2 of JEP 302, that is going to add underscore as a special character to denote unused parameters in lambda expressions.

Treatment of underscores

In many languages, it is common to use an underscore (_) to denote an unnamed lambda parameter (and similarly for method and exception parameters):

BiFunction<Integer, String, String> biss = (i, _) -> String.valueOf(i);

This allows stronger static checking of unused arguments, and also allows multiple arguments to be marked as unused. However, because underscore was a valid identifier as of Java 8, compatibility required us to take a more indirect path to getting to where underscore could serve this role in Java. Phase 1 was forbidding underscore as a lambda formal parameter name in Java 8 (this had no compatibility consequence, since lambdas did not exist previously) and a warning was issued for using underscore as an identifier in other places. Phase 2 came in Java 9, when this warning became an error. We are now free to complete the planned rehabilitation of underscore to indicate an unused lambda, method, or catch formal parameter.

answered Jul 9, 2018 at 22:34

7 Comments

This usage is discussed by Brian Goetz in his Devoxx talk of 2017-11 about Project Amber.
We currently use $ for this purpose.
I'm on Java 14 now, and I still cannot use an underscore as an unnamed lambda parameter. Whatever the JCP set out to achieve, it appears they have achieved the opposite.
@Frans Note that the JEP is (as of today) only in candidate stage. It has not been completed yet. For more details on the JEP process see JEP 1
@AlexandredeChampeaux The original comment was from 2014 so it seemed reasonable to assume that both phases would have been completed. I don't quite see the merits of disallowing an underscore only to allow it again later (albeit with slightly different semantics) but I guess the wise men had their reasons.
|
7

Java Language Changes for Java SE 9 https://docs.oracle.com/javase/9/language/toc.htm#JSLAN-GUID-16A5183A-DC0D-4A96-B9D8-AAC9671222DD

From Java 9, the _ character cannot be used as an identifier anymore, not just within the lambda context

The underscore character is not a legal name.

If you use the underscore character ("_") an identifier, your source code can no longer be compiled.

answered Sep 27, 2017 at 11:48

Comments

5

tl;dr

As Java evolves, the underscore character takes on a special new meaning: elide the type and name of a variable or record component.

To quote JEP 456:

Enhance the Java programming language with unnamed variables and unnamed patterns, which can be used when variable declarations or nested patterns are required but never used. Both are denoted by the underscore character, _.

Underscore = eliding type and name

In the feature Unnamed Variables & Patterns new to Java 22, we see the underscore character used to:

  • elide the type and name of a record component in pattern matching.
  • elide the identifier which follows the type or var in a type pattern.

Underscore as identifier disallowed in Java 9+

The underscore character (_) is code point 95 decimal, 5F hex. Formally named LOW LINE in the Unicode standard.

Using underscore as an identifier throws:

  • a warning in Java 8.
  • an error in Java 9+.

See: JEP 213: Milling Project Coin.

Ryan Leach
4,5006 gold badges40 silver badges85 bronze badges
answered May 25, 2023 at 2:35

1 Comment

I don't know people who normally calls underscore a "LOW LINE" xD

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.