JShell
is helpful for exploratory programming for calculations, error checking, building gui on the fly.
1. Start
PS F:\> jshell | Welcome to JShell -- Version 11.0.14 | For an introduction type: /help intro
jshell> /help intro
2. variable assignment
jshell> int x = 8 x ==> 8
3. expression evaluation
jshell> x + x $2 ==> 16 jshell> y = 2* x | Error: | cannot find symbol | symbol: variable y | y = 2* x | ^ jshell> float y = 2 * x y ==> 16.0 jshell> x = 2 * y | Error: | incompatible types: possible lossy conversion from float to int | x = 2 * y | ^---^ jshell> System.out.println("Hello"); Hello
4. Definitions
jshell> String twice(String s) { ...> return s + s; ...> } | created method twice(String) jshell> twice("Ocean") $6 ==> "OceanOcean" jshell> String twice(String s) { ...> return "Twice: " + s; ...> } | modified method twice(String) jshell> twice("Forest"); $8 ==> "Twice: Forest"
and a more involved example
jshell> double volume(double radius) { ...> return 4.0 / 3.0 * PI * cube(radius); ...> } | created method volume(double), however, it cannot be invoked until variable PI, and method cube(double) are declared jshell> double PI = 3.14145926535 PI ==> 3.14145926535 jshell> volume(2) | attempted to call method volume(double) which cannot be invoked until method cube(double) is declared jshell> double cube(double x) {return x * x * x; } | created method cube(double) jshell> volume(2) $17 ==> 33.5088988304 jshell> BigDecimal PI = new BigDecimal("3.141592653589793238462643383") PI ==> 3.141592653589793238462643383 jshell> volume(2) | attempted to call method volume(double) which cannot be invoked until this error is corrected: | bad operand types for binary operator '*' | first type: double | second type: java.math.BigDecimal | return 4.0 / 3.0 * PI * cube(radius); | ^------------^
5. Reassignment
jshell> String x x ==> null jshell> int x = 10 x ==> 10 jshell> String x x ==> null jshell> x x ==> null
6. Error checking
Find out the exceptions specific to an operation to be handled.
jshell> int divide(int x, int y) { ...> return x / y; ...> } | created method divide(int,int) jshell> divide(5,0) | Exception java.lang.ArithmeticException: / by zero | at divide (#20:2) | at (#21:1) jshell> System.identityHashCode(PI) $23 ==> 1987083830
7. Build GUI
Build a simple visible frame 300x200
jshell> JFrame frame = new JFrame("Demo") frame ==> javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden, ... tPaneCheckingEnabled=true] jshell> frame.setSize(300, 200) jshell> frame.setVisible(true) jshell> Graphics graphic = frame.getGraphics() graphic ==> sun.java2d.SunGraphics2D[font=java.awt.Font[famil ... java.awt.SystemColor[i=9]]
8. Program data
List all the variables
jshell> /vars | int $2 = 16 | float y = 16.0 | String $6 = "OceanOcean" | String $8 = "Twice: Forest" | String x = null | double $15 = 0.0 | double $17 = 33.5088988304 | BigDecimal PI = 3.141592653589793238462643383 | int $23 = 1987083830 | JFrame frame = javax.swing.JFrame[frame0,0,0,300x200,hidden,layout=java.awt.BorderLayout,title=Demo,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,8,31,284x161,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true] | Graphics graphic = sun.java2d.SunGraphics2D[font=java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12],color=java.awt.SystemColor[i=9]]
List all the methods in the current JShell
session.
jshell> /methods | String twice(String) | double volume(double) | which cannot be invoked until this error is corrected: | bad operand types for binary operator '*' | first type: double | second type: java.math.BigDecimal | return 4.0 / 3.0 * PI * cube(radius); | ^------------^ | double cube(double) | int divide(int,int)
9. Load libraries
Loading libraries with --class-path
option.
PS F:\> jshell --class-path 'F:/Repo/sds/systemds/target' | Welcome to JShell -- Version 11.0.14 | For an introduction type: /help intro jshell> import org.apache.sysds.*;
Top comments (0)