• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Lamda parameter with _(underscore) variable name in Java 8/9

 
Ranch Hand
Posts: 108
2
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Authors,

I've written a tiny program that implements simple lambda. I get an error as shown in below.


As described in JDK 8, it is true.
In Java 8, The lambda identifier can not be declared with _(underscore) identifier such as : _ -> 0

For e.g,



In JDK 9, use of _ (underscore) as an identifier causes an compile-time error.
In the following line   //    t(_ -> 0);  //line 1, it will be occurred an error while it is uncommented.

The highlight of the above code such that this rule is applied only to '_', but not for other valid identifiers such as '$'.

The question is to know here, "would the underscore be in reserved collection or not in JDK 8 or further?


Thank you for your reply in advance.







 
Noorul Hameed
Ranch Hand
Posts: 108
2
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Noorul Hameed wrote:  In JDK 9, use of _ (underscore) as an identifier causes an compile-time error.
In the following line   //    t(_ -> 0);  //line 1, it will be occurred an error while it is uncommented.



Typo error is there: please consider line 2 instead of line 1.

thanks
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The underscore alone is a keyword, as you will find here. It is never used; it is a keyword to prevent it from being used as an identifier. Up to Java8 people used _ as an identifier, which was regarded as confusing code, so that is now prohibited. Try compiling your code with Java8 or older. The use of _ alone to the left of -> was prohibited in λs in Java8.
 
Saloon Keeper
Posts: 15488
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the reason is that in many functional languages, underscore is used to indicate that you don't care about the value of a certain parameter because you're not going to use it. Maybe the Java designers wanted to keep this option open until they decided what they really wanted to do, so they prevented people from using it as an identifier so they don't have to worry about backwards compatibility.

Maybe in the future they will allow the use of underscore as a parameter name in lambda expressions, while prohibiting their use in lambda expression bodies, thereby enforcing the convention that underscores indicate the parameter is not used.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe; that might be what is hinted at in the Java8 JLS link I posted.
 
Stephan van Hulst
Saloon Keeper
Posts: 15488
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't find any hints of the kind.
 
author
Posts: 200
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually didn't know about underscore as I've never tried to use it as an identifier!

I personally think it's a really bad idea to use _ as an identifier... I'd recommend avoiding that.

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I couldn't find any hints of the kind.

That JLS section wrote: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.

Just a hint.
 
Ranch Hand
Posts: 72
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe; that might be what is hinted at in the Java8 JLS link I posted.


The link you've posted is Java10 JLS not Java8 JLS

Stephan van Hulst wrote:I couldn't find any hints of the kind.


The hint is in the "Identifiers paragraph" here with very small font ;)
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Ibrahim wrote:. . . The link you've posted is Java10 JLS not Java8 JLS . . .

Good catch , but the second link I posted, this one, is to the Java8 JLS. The Java9/10 equivalent pages include this:-

JLS §15.27.1 wrote: In Java SE 8, the use of _ as the name of a lambda parameter was forbidden, and its use discouraged as the name for other kinds of variable (§4.12.3). As of Java SE 9, _ is a keyword (§3.9) so it cannot be used as a variable name in any context.

 
Ahmed Ibrahim
Ranch Hand
Posts: 72
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
the second link I posted, this one, is to the Java8 JLS


yes, i found the hint you've mentioned in the second link "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."

but with very small font
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . but the use of _ might be permitted again in Java15; who knows. They might find some potential use for it to fulfil the bit about special syntax.
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I think the reason is that in many functional languages, underscore is used to indicate that you don't care about the value of a certain parameter because you're not going to use it. Maybe the Java designers wanted to keep this option open until they decided what they really wanted to do, so they prevented people from using it as an identifier so they don't have to worry about backwards compatibility.

Maybe in the future they will allow the use of underscore as a parameter name in lambda expressions, while prohibiting their use in lambda expression bodies, thereby enforcing the convention that underscores indicate the parameter is not used.


This agrees with what is mentioned in JEP 302: Lambda Leftovers:

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.
reply
    Bookmark Topic Watch Topic
  • New Topic