0

As i am aware, the _ for unnamed variables has been a preview feature [java:21] later released with JDK 22. This is the code in question

 char[] _ = { 'b', 'o', 'b' }; 

However i get this error

[ERROR] '_' is a keyword from source level 9 onwards, cannot be used as identifier 

Now i had assumed that its a issue with the JDK version Maven is set to build with so i changed pom.xml

 <properties> <maven.compiler.source>22</maven.compiler.source> <maven.compiler.target>22</maven.compiler.target> </properties> 

And that hasn't resolved the issue - so i rather thought of checking it with mavens --show-version:

Maven home: [maven path] Java version: 22.0.1, vendor: Amazon.com Inc., runtime: [my path]/java/22.0.1-amzn Default locale: [locale] OS name: [os info] 

for the sake of simplicity i've removed some info

So how do i go about resolving this error?

18
  • @SotiriosDelimanolis What im trying to do is to use the _ keyword in Java. I don't understand your second question - if its a java 22 feature then it will work(without the preview mode) with a java 22 compiler, what seems to be wrong with that? Cause by default its going to use Java 17 which has this keyword but its not entirely the same. Commented Jul 9, 2024 at 15:20
  • @SotiriosDelimanolis JEP 456 and as i am aware there is virtually no limitation of using this anywhere - rather its not "good practice" per say. However a Youtube video does this same thing however using a non-primitive so i decided to use String and a custom similar class as the Youtuber is using and i still get the error. now i sorted to using the full stack trace with -e but im sure its 100% the _ Commented Jul 9, 2024 at 15:34
  • And yes i have also tried it with lambda and try/catch as the article provided(as a good practice use case but still). Commented Jul 9, 2024 at 15:38
  • 2
    The _ is defined as variable name which is not required to be named because it's ignored... openjdk.org/jeps/456 shows possible usages; It is not allowed to be used an identifier alone... Examples: github.com/khmarbaise/jdk22/blob/main/src/test/java/com/soebes/… Commented Jul 9, 2024 at 15:58
  • @khmarbaise Perfect. thanks for proving my point. That code base uses some preview features if im right. but thats besides the point im getting a crap ton of [ERROR] '_' is a keyword from source level 9 onwards, cannot be used as identifier errors Commented Jul 9, 2024 at 16:07

1 Answer 1

0

You said:

i included the line of code that produces the error. put it in your psvm or any other function and it will produce this same error.

Nope, no such error. Works as documented.

This compiles and runs:

char[] _ = { 'b' , 'o' , 'b' }; 

Full example:

package work.basil.example.lang; import java.time.Instant; public class Underscore { public static void main ( String[] args ) { System.out.println( Runtime.version( ) ); int[] _ = { 1 , 2 , 3 }; String _ = Underscore.getAlice( ); char[] _ = { 'b' , 'o' , 'b' }; char[] _ = Underscore.getChars( ); System.out.println( "End of `main`. " + Instant.now( ) ); } public static String getAlice ( ) { System.out.println( "Getting Alice at " + Instant.now( ) ); return "Alice"; } public static char[] getChars ( ) { System.out.println( "Getting chars at " + Instant.now( ) ); return new char[] { 'b' , 'o' , 'b' }; } } 

When run:

22.0.1+10 Getting Alice at 2024-07-09T19:31:16.269891Z Getting chars at 2024-07-09T19:31:16.272960Z End of `main`. 2024-07-09T19:31:16.273040Z 

By the way, with modern tooling you can collapse your source & target elements to one release element: <maven.compiler.release>22</maven.compiler.release>.

Here is my complete POM.

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>work.basil.example</groupId> <artifactId>Ex</artifactId> <version>1.0-SNAPSHOT</version> <name>Ex</name> <!-- FIXME change it to the project's website --> <url>https://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.release>22</maven.compiler.release> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.10.2</version> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>2.2.224</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.4.0</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>4.11.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jetbrains/annotations --> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations</artifactId> <version>24.1.0</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (can be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.3.2</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.4.1</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>3.1.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>3.1.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>4.0.0-M13</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.5.0</version> </plugin> </plugins> </pluginManagement> </build> </project> 

By the way, avoid char type. That type has been essentially broken since Java 2, and legacy since Java 5. As a 16-bit value, char is physically incapable of representing most characters. If using char, one emoji could ruin your whole day.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mate - i ran this without maven and it works perfectly. i assume its some weird maven thing and i'll try to resolve it. Also thanks for your other suggestions
You can see it the other way round; when you know beforehand that you’re only operating with ASCII values, using char[] is wasting memory, as you could use byte[] instead. But for variables named _, you can be sure that their data type won’t cause many problems anyway…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.