Java_Language

Java Tutorial2D Graphics in JavaJava Stack-Walking APIClass - Java ReflectionCommon Java PitfallsDocumenting Java CodeGenerating Java CodeInstalling Java (Standard Edition)Java AgentsJava Alternative CollectionsJava AnnotationsJava Apache Commons LangJava AppDynamics and TIBCO BusinessWorks Instrumentation for Easy IntegrationJava AppletsJava ArraysJava AssertingJava Atomic TypesJava AudioJava AutoboxingJava Basic Control StructuresJava BenchmarksJava BigDecimalJava BigIntegerJava Bit ManipulationJava BufferedWriterJava ByteBufferJava Bytecode ModificationJava C++ ComparisonJava Calendar and its SubclassesJava Character encodingJava Choosing CollectionsJava Classes and ObjectsJava ClassloadersJava Collection Factory MethodsJava CollectionsJava Command line Argument ProcessingJava Comparable and ComparatorJava Compiler - javacJava CompletableFutureJava Concurrent CollectionsJava Concurrent Programming (Threads)Java Console I/OJava ConstructorsJava Converting to and from StringsJava Creating Images ProgrammaticallyJava Currency and MoneyJava Date ClassJava Dates and Time (java.time.*)Java Default MethodsJava deploymentJava Dequeue InterfaceJava Disassembling and DecompilingJava Dynamic Method DispatchJava Editions, Versions, Releases and DistributionsJava EncapsulationJava Enum MapJava Enum starting with numberJava EnumsJava EnumSet classJava Exceptions and exception handlingJava Executor, ExecutorService and Thread poolsJava ExpressionsJava File I/OJava FileUpload to AWSJava Floating Point OperationsJava Fluent InterfaceJava FTP (File Transfer Protocol)Java Functional InterfacesJava GenericsJava Getters and SettersJava HashtableJava HttpURLConnectionJava Immutable ClassJava Immutable ObjectsJava InheritanceJava InputStreams and OutputStreamsJava InterfacesJava Iterator and IterableJava JavaBeanJava JAX-WSJava JAXBJava JMXJava JNDIJava JShellJava Just in Time (JIT) compilerJava JVM FlagsJava JVM Tool InterfaceJava Lambda ExpressionsJava LinkedHashMapJava List vs SET



Java JShell

From WikiOD

JShell is an interactive REPL for Java added in JDK 9. It allows developers to instantly evaluate expressions, test classes, and experiment with the Java language. Early access for jdk 9 can be obtained from: http://jdk.java.net/9/

Syntax[edit | edit source]

  • $ jshell — Start the JShell REPL
  • jshell> /<command> — Run a given JShell command
  • jshell> /exit — Exit JShell
  • jshell> /help — See a list of JShell commands
  • jshell> <java_expression> - Evaluate the given Java expression (semicolon optional)
  • jshell> /vars OR /methods OR /types — See a list of variables, methods, or classes, respectively.
  • jshell> /open <file> — read a file as input to the shell
  • jshell> /edit <identifier> — edit a snippet in the set editor
  • jshell> /set editor <command> — set the command to be used to edit snippets using /edit
  • jshell> /drop <identifier> — delete a snippet
  • jshell> /reset — Reset the JVM and delete all snippets

Remarks[edit | edit source]

JShell requires the Java 9 JDK, which can currently (March 2017) be downloaded as early access snapshots from jdk9.java.net. If, when you try to run the jshell command, you get an error beginning with Unable to locate an executable, make sure JAVA_HOME is set correctly.

Default Imports[edit | edit source]

The following packages are imported automatically when JShell starts:

import java.io.*
import java.math.*
import java.net.*
import java.nio.file.*
import java.util.*
import java.util.concurrent.*
import java.util.function.*
import java.util.prefs.*
import java.util.regex.*
import java.util.stream.*

Editting Snippets[edit | edit source]

The basic unit of code used by JShell is the snippet, or source entry. Every time you declare a local variable or define a local method or class, you create a snippet whose name is the identifier of the variable/method/class. At any time, you can edit a snippet you have created with the /edit command. For example, let's say I have created the class Foo with a single, method, bar:

jshell> class Foo {
   ...> void bar() {
   ...> }
   ...> }

Now, I want to fill in the body of my method. Rather than rewrite the entire class, I can edit it:

jshell> /edit Foo

By default, a swing editor will pop up with the most basic features possible. However you can change the editor that JShell uses:

jshell> /set editor emacs
jshell> /set editor vi
jshell> /set editor nano
jshell> /set editor -default

Note that if the new version of the snippet contains any syntax errors, it may not be saved. Likewise, a snippet is only created if the original declaration/definition is syntactically correct; the following does not work:

jshell> String st = String 3
//error omitted
jshell> /edit st
|  No such snippet: st

However, snippets may be compiled and hence editable despite certain compile-time errors, such as mismatched types—the following works:

jshell> int i = "hello"
//error omitted
jshell> /edit i

Finally, snippets may be deleted using the /drop command:

jshell> int i = 13
jshell> /drop i
jshell> System.out.println(i)
|  Error:
|  cannot find symbol
|    symbol:   variable i
|  System.out.println(i)
|

To delete all snippets, thereby reseting the state of the JVM, use \reset:

jshell> int i = 2

jshell> String s = "hi"

jshell> /reset
|  Resetting state.

jshell> i
|  Error:
|  cannot find symbol
|    symbol:   variable i
|  i
|  ^

jshell> s
|  Error:
|  cannot find symbol
|    symbol:   variable s
|  s
|  ^

Entering and Exiting JShell[edit | edit source]

Starting JShell[edit | edit source]

Before trying to start JShell, make sure your JAVA_HOME environment variable points to a JDK 9 installation. To start JShell, run the following command:

$ jshell

If all goes well, you should see a jshell> prompt.

Exiting JShell[edit | edit source]

To exit JShell, run the following command from the JShell prompt:

jshell> /exit

Expressions[edit | edit source]

Within JShell, you can evaluate Java expressions, with or without semicolons. These can range from basic expressions and statements to more complex ones:

jshell> 4+2
jshell> System.out.printf("I am %d years old.\n", 421)

Loops and conditionals are fine, too:

jshell> for (int i = 0; i<3; i++) {
   ...> System.out.println(i);
   ...> }

It is important to note that expressions within blocks must have semicolons!

Methods and Classes[edit | edit source]

You can define methods and classes within JShell:

jshell> void speak() {
   ...> System.out.println("hello");
   ...> }

jshell> class MyClass {
   ...> void doNothing() {}
   ...> }

No access modifiers are necessary. As with other blocks, semicolons are required inside of method bodies. Keep in mind that, as with variables, it is possible to redefine methods and classes. To see a list of methods or classes, enter /methods or /types at the JShell prompt, respectively.

Variables[edit | edit source]

You can declare local variables within JShell:

jshell> String s = "hi"
jshell> int i = s.length

Keep in mind that variables can be redeclared with different types; this is perfectly valid in JShell:

jshell> String var = "hi"
jshell> int var = 3

To see a list of variables, enter /vars at the JShell prompt.

Credit:Stack_Overflow_Documentation