Firstly, we need JDK 12 installed in our system with the path variable or JAVA_HOME set. If you have successfully installed JDK 12, execute the following command
The following result will you get in the command prompt.
Step 2
Download the Visual Studio Code as shown below.
After a successful download and installation, you will see the home page of the editor.
Step 3
Create a Java file named
Java12Demo.java and open it from the File tab. Immediately, after you open the Java file, it recommends installing the Java extension pack and a debugger as shown in the image below.
Now, you can see the Java extension pack. Click on the "Install" button. Within seconds, it will install completely.
Step 4
It is now the time of writing code in our newly created file
Java12Demo.java. Let's have a look at the below code.
- public class Java12Demo {
- public static void main(String[] args) {
- Java12Demo demo = new Java12Demo();
- demo.enhancedSwitchCase();
- String convertedWord = demo.returnConvertedWord(2);
- System.out.println(convertedWord);
- }
- public String returnConvertedWord(int integer) {
- System.out.println("Switch Statement returning a value in this method");
- String word =
- switch (integer) {
- case 1 -> "One";
- case 2 -> "Two";
- case 3 -> "Three";
- default -> "N/A";
- };
- return word;
- }
- public void enhancedSwitchCase() {
- System.out.println("\nEnhanced Switch Statement: case L-> syntax");
- final int integer = 2;
- String numericString;
- switch (integer) {
- case 1 -> numericString = "One";
- case 2 -> numericString = "Two";
- case 3 -> numericString = "Three";
- default -> numericString = "N/A";
- }
- System.out.println("\t" + "Given Integer is : " + " ==> " + numericString);
- }
- }
There are two methods here - one is only visualizing the syntax of new switch statement like
case arrow syntax as shown above. Now, see the method named
returnConvertedWord(). In this method, we can clearly see that the switch is returning a string value and we are printing it. See, there is no need for a break statement. The statement after arrow runs.
Compiling
Note - this is a preview version, therefore, the compiling is not easy as it usually is. There are some flags we have to add in the command as provided by Oracle. Flags are shown in the below commands.
When we compile the above program, it gives an error
"switch rules are a preview feature and are disabled by default". This error is with JDK 12 javac compiler. So, we have to enable the preview feature. This compiler is giving us a beautiful hint, i.e.,
"use --enable-preview to enable switch rules."
So applying the given hint in our command will look like this.
Now again, another error comes saying
"--enable-preview must be used with either -source or --release". Now, I have decided to go with the --release 12. Let us see the compilation result with the release version.
We can see that this time, no error is given by JDK 12 compiler but some notes are given as
"Java12Demo.java uses preview language features". This clearly indicates that we have to recompile with the
-Xlint: preview flag in the command.
When the command is passed with -Xlint: preview flag, the JDK 12 compiler compiles the program without any issue but it points a warning to every line saying
"switch rules are a preview feature and may be removed in a future release". Now, the program is error-free so it is time to run our program. Let's see what happens.
If we run the command, simply nothing happens. We have to pass
-cp build because of JDK 12 compiler. So, passing the
-cp build flag to compiler gives the linkage error.
It is giving LinkageError while loading class; also giving us hint what we have to enable to smooth run the command. We have to enable the "--enable-preview". After passing this flag to JDK 12 compiler, the output shows up with no issue.
Now, the first method illustrates the case L-> syntax and output is Two. The third line is returning a value, i.e., Two, which comes from the second method as shown in the code.