Architecture & DesignUsing JShell in Java 9 in NetBeans 9.0, Part 5

Using JShell in Java 9 in NetBeans 9.0, Part 5

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

JShell is an interactive tool introduced in Java 9 to run and test code snippets. JShell as used in NetBeans 9.0 was discussed is earlier articles “Using JShell in Java 9 in NetBeans 9.0, Part 1,” “Using JShell in Java 9 in NetBeans 9.0, Part 2,” “Using JShell in Java 9 in NetBeans 9.0, Part 3,” and “Using JShell in Java 9 in NetBeans 9.0, Part 4.” In this continuation article, we shall run other kinds of code snippets in JShell. This article has the following sections:

Setting the Environment

Download and install the following software:

Open the C:incubating-netbeans-java-9.0-binnetbeansetcnetbeans.conf file in a text editor and set the JDK environment variable netbeans_jdkhome.

netbeans_jdkhome="C:Program FilesJavajdk-9.0.4"

Start the JShell by selecting Tools>Open Java Platform Shell, as shown in Figure 1.

Tools>Open Java Platform Shell
Figure 1: Tools>Open Java Platform Shell

The Java Platform Shell gets launched and the command prompt gets displayed, as shown in Figure 2.

Java Platform Shell Command Prompt
Figure 2: Java Platform Shell Command Prompt

Using Enum Snippets

Enum is a special data type and should be used to define a set of constants. Enums may be run in JShell at the top level without the context of a class. As an example, create an enum JAVA_VERSION with values as the various JDK versions.

public enum JAVA_VERSION {
   JavaSE7, JavaSE8, JavaSE9, JavaSE10, JavaSE11
}

Run the code snippet in JShell and an enum called JAVA_VERSION gets created, as shown in Figure 3.

Enum JAVA_VERSION Created
Figure 3: Enum JAVA_VERSION Created

Create a variable of type JAVA_VERSION and set its value to one of the enum values.

JAVA_VERSION version=JAVA_VERSION.JavaSE9;

Use the variable of type enum with a switch statement to output a statement. The break statement is used to break out of the switch statement when a match is found.

switch (version) {
   case JavaSE7:
      System.out.println("Version is Java SE 7.");
      break;
   case JavaSE8:
      System.out.println("Version is Java SE 8.");
      break;
   case JavaSE9:
      System.out.println("Version is Java SE 9.");
      break;
   case JavaSE10:
      System.out.println("Version is Java SE 10.");
      break;
   case JavaSE11:
      System.out.println("Version is Java SE 11.");
      break;
   default:
      System.out.println("Java version is not defined.");
      break;
}

Run the preceding two statements in JShell and the System.out.println statement that matches the enum value as selected by switch gets output, as shown in Figure 4.

Output from using switch Statement with enum
Figure 4: Output from using switch Statement with enum

Using Control Flow Statements

Several categories of control flow statements are defined: decision-making statements (if-then, if-then-else, and switch), looping statements (for, while, and do-while) and branching statements (break, continue, and return). We shall run code snippets for each of these in JShell. We have already run a code snippet containing switch and break and shall run more code snippets containing break.

First, run an example of using the if statement.

int x=5;
if(x>=5){
   System.out.println("x is greater than or equal to 5");
}

Because the test expression evaluates to true, the statement in the if block runs. The output is shown in Figure 5.

Running an if Statement
Figure 5: Running an if Statement

Next, develop a code snippet to run an if-then-else statement.

int x=5;
if(x<5){
   System.out.println("x is greater than or equal to 5");
}else{
   System.out.println("If x is an integer, it must be greater than
      or equal to 5 as it is not less than 5");
}

The test expression evaluates to false and the statement within the else block runs (see Figure 6).

Running an if-then-else Statement
Figure 6: Running an if-then-else Statement

Next, run a code snippet for a while statement.

int x=5;
while(x<=10){
   System.out.println("x is "+x);
   x++;
}

The output from the while statement is shown in Figure 7.

Output from the while Statement
Figure 7: Output from the while Statement

An example of a do-while statement is as follows.

int x=5;
do{
   System.out.println("x is "+x);
   x++;
}while(x<=10);

The output from the do-while is shown in Figure 8. The control flow statements are run at the top level, independent of other statements and do not need the context of a class.

Output from the do-while Statement
Figure 8: Output from the do-while Statement

Here an example of the for statement output integer values from 1 to less than 10.

for(int i=1; i<10; i++){
   System.out.println("x is: " + i);
}

Integer values from 1 to 9 get output, as shown in Figure 9.

Running a for Statement
Figure 9: Running a for Statement

The break statement may be used with the for statement as follows.

for(int i=1; i<10; i++){
   System.out.println("x is: " + i);
   break;
}

The output from the for loop is shown in Figure 10.

Output from the for loop with break
Figure 10: Output from the for loop with break

Next, run a do-while statement that includes a break statement.

int x=5;
do{
   System.out.println("x is "+x);
   x++;
   break;
}while(x<=10);

The do-while loop is broken after just one iteration, as shown in the output (see Figure 11).

Output from do-while with break
Figure 11: Output from do-while with break

A while loop with a break statement is as follows.

int x=5;
while(x<=10){
   System.out.println("x is "+x);
   x++;
   break;
}

The while loop breaks after just one iteration, as shown in Figure 12.

Output from while with break
Figure 12: Output from while with break

We have run code snippets with unlabeled breaks. A labeled break may be used as follows to terminate an outer labeled for loop statement from an inner for loop.

label1:
   for (int i = 0; i <10; i++) {
      for (int j = 0; j <5;j++) {
         System.out.println("i is "+i);
         System.out.println("j is "+j);
         if(i>j){ break label1;}

      }
   }

Different values of j are output in an inner for loop for different values of i in an outer for loop. When i becomes greater than j, the label1 statement is terminated, as indicated by the output in Figure 13.

Using labeled break Statement with for loops
Figure 13: Using labeled break Statement with for loops

Whereas the labeled break statement is used to terminate a labeled looping statement, the labeled continue statement is used to transfer control to a labeled statement. An example labeled continue statement is as follows.

label1:
   for (int i = 0; i <10; i++) {
      System.out.println("i is "+i);
      for (int j = 0; j <5;j++) {

         System.out.println("j is "+j);
         if(i==j){ continue label1;}

      }
   }
As shown in the output in Figure 14, when i==j the control is transferred to the label1 statement and the next iteration of the outer forloop is run.

Example of the labeled continue statement
Figure 14: Example of the labeled continue statement

An example of a unlabeled continue statement is as follows. Control is transferred to the next iteration of the while loop when x==7.

int x=5;
while(x<=10){

   if(x==7){x++;continue;}
   System.out.println("x is "+x);
   x++;
}

As shown in the output in Figure 15, the value of x is 7 is not output because the control flow gets transferred to the next iteration of while.

Example of using while with unlabeled continue
Figure 15: Example of using while with unlabeled continue

An unlabeled continue may be used with a for loop as follows. If i==5, the control is transferred to the next iteration of the for loop.

for(int i=1; i<10; i++){
   if(i==5){continue;}
   System.out.println("i is: " + i);

}

The if statement must be before the System.out.println statement to skip outputting a value of x for i==5. As shown in Figure 16, the value of i is 5 is not output.

Using Unlabeled continue with for
Figure 16: Using Unlabeled continue with for

Next, we shall run a code snippet as an example of the return statement. Create a method getX(int) that outputs an int value. Invoke the method with an arg of 1.

int getX(int y){
   int x=y++;
   return x;
}
getX(1);

Run the code snippet and the value of 1 is returned by the getX method and assigned to a variable, as shown in Figure 17.

Running a Code Snippet for a Method that returns an integer
Figure 17: Running a Code Snippet for a Method that returns an integer

Next, run an example of a method with the void return type. The return statement does not return a value.

void getX(int y){
   int x=y++;
   return;
}

Invoke the function with an arg of 1.

getX(1);

No value is returned by the method, as shown in Figure 18.

Running a Code Snippet for a Method with void Return Type
Figure 18: Running a Code Snippet for a Method with void Return Type

Conclusion

In this article, we discussed using JShell to run code snippets for the enum data type. We also ran code snippets for the different control flow statements if-then, if-then-else, for, while, do-while, break, continue, and switch. All of these code snippets were run at the top level without creating a class definition. In the final article of this series, we shall run lambda expressions in JShell. We shall also discuss saving code snippets to a file and loading code snippets from a file.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories