The course is part of this learning path
This course introduces you to the Java Shell tool, an interactive tool for learning the Java programming language and prototyping with Java code.
- [Instructor] Okay, welcome back. In this lecture, we'll explore the JShell tool, which, as already mentioned, can be used as an interactive tool for learning the Java programming language, and can be used to prototype your own Java code. We'll cover items such as introducing you to JShell, running expressions in JShell, importing packages, defining methods and types within JShell, using the JShell editor, and saving and loading state. For many years, programming languages required the compiler to compile code before it could be executed. Additionally, other basic and primitive tools were provided to test code snippets without the need to write an entire application. However, in Java, such a tool was never readily available. If code snippets had to be tested, developers were forced to develop a complete Java class, compile it, and execute it within the runtime. Every time the statement under test was changed, the code had to be recompiled before it could be executed again. Java 9 finally provides us with a shell, which allows us to test snippets of code without this compilation overhead. Program elements can be entered one at a time with immediate results returned for the last operation. The JShell utility is found in the Java home/bin directory. Starting it is done simply by running jshell from a shell session. By using the -v command line argument, the tool can be started in verbose mode, resulting in more detailed information being displayed within the shell when executing commands. The verbose mode can also be enabled and disabled from within the tool by setting the feedback property. Once the tool is running, any valid Java expression can be entered. Once the Enter key is pressed, the expression is immediately evaluated and the result is shown.
When the expression being executed returns a value, this value is automatically assigned to an internal variable. When the tool is running in verbose mode, the type of the variable that was created will also be displayed. Instead of relying on internal variables, named variables can also be declared, just like it would be done in a normal Java class. Once such a variable has been declared, it can also be used in other expressions or when the type is an object reference used to invoke methods on the object. To get an overview of the variables that are currently in use, the /vars command can be used. Default package imports. Several Java packages are imported by default. Therefore, classes do not require fully qualified names. A list of imports is displayed using the /imports command. JShell automatically imports the classes from the most commonly used packages in Java EC. As a result, you do not need to have to specify the fully qualified names of the classes. For example, the java.util package. Importing classes. When classes from other packages are required, you can use either the fully qualified name of those classes or import additional packages and types using the import statement. Using the import statement, complete packages or individual types can be imported. Keep in mind that when multiple classes with the same name are being imported, the type imported the last will be used when only the class name is used. In other words, every import overrides imports of a class with the same name from a different package. Complete method definitions can be defined and used when using JShell. Once a method has been defined, it can be invoked just like you would in a normal Java class. JShell also provides the /methods command to display the methods that have been declared within the JShell context. Once a method has been defined, changes can be made to the implementation of that method. When the same signature is used, it will override the implementation defined earlier. Even complete class definitions can be defined within the JShell context. The editor allows for the definitions to be defined on multiple modes. It expects an open an opening curly bracket to be accompanied by a closing curly bracket. As long as it does not discover a matching pair of curly brackets, it will assume that you are not yet done with defining the class or method. Once class definitions have been added to the context, they can be instantiated just like any other type in the Java programming language. JShell provides the /types command to display all types that are currently available and context. The /edit command can be used to start an external editor to modify existing class or method definitions. Even though this external editor does not provide things like code highlighting or code completion, it does, however, allow you to quickly modify a class or method you defined earlier without having to completely rewrite the entire class or method. Once the Accept button is clicked in the editor, the statements in the editor will be evaluated and you will be returned back to the JShell environment. When any errors are discovered, these will be displayed and the type or method will not be modified. Either the /exit command or the Control + D command can be used to exit the tool. When you do not want to leave the tool but would like to get rid of all variables, methods, and types, the /reset command can be used. The Tab key can be used to provide code suggestions within JShell. While most IDEs use the Control + space command to initiate code completion, JShell uses the Tab key to provide suggestions on the types and methods available in context or on the possible methods that can be invoked on an object reference. When non-JDK classes are needed in the shell, these classes have to be available on the class path of the tool. The class path can be set when the shell is started or set using the /env command. With the introduction of Java 9, we will start to move away from having types defined on the class path. Instead, we will use and develop modules to allow for better dependency management of types within an application. When modules need to be referenced from within JShell, the module path can also be set. All code that was entered in the JShell context can be stored to a file using the /save command. At a later stage, this file can be reused or reloaded back into the JShell context using the /open command. Okay, the answers to the above questions are, one, to quickly test snippets of Java code. Two, read, evaluate, print, and loop. Okay, that completes this lecture. Go ahead and close it, and we'll see you shortly in the next one.
About the Author
Jeremy is the DevOps Content Lead at Cloud Academy where he specializes in developing technical training documentation for DevOps.
He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 20+ years. In recent times, Jeremy has been focused on DevOps, Cloud, Security, and Machine Learning.
Jeremy holds professional certifications for both the AWS and GCP cloud platforms.