Run Java Code In Visual Studio Code

Introduction

 
This article mainly focuses on the new switch statement that comes with the newer release of the Java language, i.e., Java 12. If you want to learn more about all the changes, you can read about it here. Now, I am taking Visual Studio Code as our editor; however, you can take any of the editors of your choice. So, what are the changes done to the switch-case statement? Let's take a look below.
  
Here are two main changes.
  • Introduction of case L -> syntax that removes the need for break statements, because only the statements next to -> are executed.
  • Switch can be an expression, so it can have a value or it can return a value
Step 1
 
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
java -version.
 
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.
  1. public class Java12Demo {  
  2.  public static void main(String[] args) {  
  3.   Java12Demo demo = new Java12Demo();  
  4.   demo.enhancedSwitchCase();  
  5.   String convertedWord = demo.returnConvertedWord(2);  
  6.   System.out.println(convertedWord);  
  7.  }  
  8.  public String returnConvertedWord(int integer) {  
  9.   System.out.println("Switch Statement returning a value in this method");  
  10.   String word =  
  11.    switch (integer) {  
  12.     case 1 -> "One";  
  13.     case 2 -> "Two";  
  14.     case 3 -> "Three";  
  15.     default -> "N/A";  
  16.    };  
  17.   return word;  
  18.  }  
  19.  public void enhancedSwitchCase() {  
  20.   System.out.println("\nEnhanced Switch Statement: case L-> syntax");  
  21.   final int integer = 2;  
  22.   String numericString;  
  23.   switch (integer) {  
  24.    case 1 -> numericString = "One";  
  25.    case 2 -> numericString = "Two";  
  26.    case 3 -> numericString = "Three";  
  27.    default -> numericString = "N/A";  
  28.   }  
  29.   System.out.println("\t" + "Given Integer is : " + " ==> " + numericString);  
  30.  }  
  31. }  
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. 
 

Conclusion

 
This article focused on the enhanced feature of the switch statement in Java 12. The most noticeable thing in this article is the absence of a break statement. The absence of break does not make it fall through and another noticeable thing is that switch is now an expression and returning a value. This is exactly the point stated by JEP 325 if the label matches the statement after arrow(->) executes, no break is required.


Similar Articles