Run Java Code In Visual Studio Code

In this article, we will learn how to setup Visual Studio code to write, test, and run Java 12 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 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.           
  4.   
  5.         Java12Demo demo = new Java12Demo();  
  6.         demo.enhancedSwitchCase(); // case L-> syntax  
  7.   
  8.         String convertedWord = demo.returnConvertedWord(2); // Here switch case is returning a value which we are printing.  
  9.         System.out.println(convertedWord);  
  10.     }  
  11.   
  12.   
  13.     public String returnConvertedWord(int integer){  
  14.   
  15.         System.out.println("Switch Statement returning a value in this method");    
  16.   
  17.         String word  =  switch (integer) { // returning value  
  18.             case 1 -> "One";  
  19.             case 2 -> "Two";  
  20.             case 3 -> "Three";  
  21.   
  22.             default -> "N/A";    
  23.               
  24.         };  
  25.         return word;  
  26.   
  27.     }  
  28.   
  29.   
  30.     public void enhancedSwitchCase(){  
  31.   
  32.         System.out.println("\nEnhanced Switch Statement: case L-> syntax");    
  33.           
  34.         final int integer = 2;    
  35.         String numericString;    
  36.         switch (integer)    
  37.         {    
  38.            case 1 -> numericString = "One";    
  39.            case 2 -> numericString = "Two";    
  40.            case 3 -> numericString = "Three";    
  41.            default -> numericString = "N/A";    
  42.         }    
  43.         System.out.println("\t" + "Given Integer is : " + " ==> " + numericString);    
  44.   
  45.     }  
  46.   
  47. }  
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 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, it 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.