42

JShell: An Interactive Shell for the Java Platform

Embed Size (px)

Citation preview

Page 1: JShell: An Interactive Shell for the Java Platform
Page 2: JShell: An Interactive Shell for the Java Platform

JShellNew Interactive Java Language Shell for JDK 9

Robert FieldJShell ArchitectCore Language and Tools GroupOracle CorporationNovember 6, 2015

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Page 3: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 3

Safe Harbor StatementThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 4: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 4

Agenda

What is JShell?

Why JShell?

Using JShell

Demo

Questions

1

2

3

4

5

Page 5: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 5

Agenda

What is JShell?

Why JShell?

Using JShell

Demo

Questions

1

2

3

4

5

Page 6: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 6

Survey

• Who has heard of JShell before JavaDay?

Page 7: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 7

Survey

• Who has heard of JShell before JavaDay?• Who has used JShell?

Page 8: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 8

Survey

• Who has heard of JShell before JavaDay?• Who has used JShell?• For whom is this new?

Page 9: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 9

What is JShell?

• Tool providing a dynamic interaction with the Java™ programming language• Read-Evaluate-Print Loop (REPL) for the Java™ platform– Type in a snippet of Java code, see the results

• Deeply integrated with JDK tool-set– Stays current and compatible

• Also, an API for use within other applications

Page 10: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 10

What is JShell NOT?

• Not a new language– “Snippets” of pure– No new syntax

• Not a replacement for the compiler• Not an IDE

Page 11: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 11

Agenda

What is JShell?

Why JShell?

Using JShell

Demo

Questions

1

2

3

4

5

Page 12: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 12

Who wants JShell?

• New to Java, new to programming– Start with expressions vs classes– Immediate feedback

• Exploring a new API or language feature– Experiment and instantly see results

• Prototyping– Incrementally write complex code

Page 13: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 13

Without JShell: Edit-Compile-Execute

• Write a full program:– Class– Imports–main method

• Cycle the whole program to understand if the behavior is correct:– Edit– Compiler or IDE– Execute

Page 14: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 14

% edit BitShift.javaclass BitShift { public static void main(String[] args) { System.out.println( -8 >> 2 ); System.out.println( -8 >>> 2 ); }}

% javac BitShift.java

% java BitShift -21073741822

% edit BitShift.java -- to print in octal so we can understand

... repeat ...

Without JShell: Edit-Compile-Execute example

Page 15: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 15

With JShell • Type in a “snippet” of code• Immediately see its behavior

Page 16: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 16

• -> -8 >> 2• | Expression value is: -2• | assigned to temporary variable $1 of type int

• -> -8 >>> 2• | Expression value is: 1073741822• | assigned to temporary variable $2 of type int

• -> printf("%o", -8 >>> 2)• 7777777776• ->

With JShell: example

Page 17: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 17

Agenda

What is JShell?

Why JShell?

Using JShell

Demo

Questions

1

2

3

4

5

Page 18: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 18

Using the JShell tool

• With JDK 9 installed, type “jshell” at the command line• The JShell tool takes two kinds of input:– “Snippets” of Java code – declaration and execution– JShell commands – information and control

• All input can be tab completed

Page 19: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 19

-> String foo = "Howdy"| Added variable foo of type String with initial value "Howdy"

Snippets of Java code: variable

Page 20: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 20

-> long fibonacci(long n) {>> if (n == 0) return 0;>> if (n == 1) return 1;>> return fibonacci(n - 1) + fibonacci(n - 2);>> }| Added method fibonacci(long)

Snippets of Java code: method

Page 21: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 21

-> interface Defloat {>> int convert(float x);>> }| Added interface Defloat

Snippets of Java code: class, interface, enum, orannotation interface

Page 22: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 22

-> fibonacci(10)| Expression value is: 55| assigned to temporary variable $4 of type long

Snippets of Java code: expression

Page 23: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 23

-> if ($4 > 20) printf("The value is %s\n", $4)The value is 55

Snippets of Java code: statement

Page 24: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 24

-> import javax.swing.*

• In fact, the only kind of Java code you cannot use is package

Snippets of Java code: import declaration

Page 25: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 25

• -> Defloat lamb = d -> Math.round(d)• | Added variable lamb of type Defloat with ...

• -> lamb.convert(2.5)• | Error:• | incompatible types: possible lossy conversion from • | double to float• | lamb.convert(2.5)• | ^-^

• -> lamb.convert((float) 2.5)• | Expression value is: 3• | assigned to temporary variable $8 of type int

Examples

Page 26: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 26

JShell Commands

• Commands are distinguished from snippets by leading slash: /• Commands – Show state– Configure display, environment, and start-up– Access history

• Have tab-completion and short-cuts

Page 27: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27

-> /help…/l or /list [all] -- list the source you have typed /seteditor <executable> -- set the external editor command to use/e or /edit <name or id> -- edit a source entry referenced by name or id/d or /drop <name or id> -- delete a source entry referenced by name or id/s or /save [all|history] <file> -- save the source you have typed/o or /open <file> -- open a file as source input/v or /vars -- list the declared variables and their values/m or /methods -- list the declared methods and their signatures/c or /classes -- list the declared classes/x or /exit -- exit the REPL/r or /reset -- reset everything in the REPL/f or /feedback <level> -- feedback information: off, concise, normal, verbose, …/p or /prompt -- toggle display of a prompt/cp or /classpath <path> -- add a path to the classpath/h or /history -- history of what you have typed /setstart <file> -- read file and set as the new start-up definitions /savestart <file> -- save the default start-up definitions to the file/? or /help -- this help message /! -- re-run last snippet /<n> -- re-run n-th snippet /-<n> -- re-run n-th previous snippet

JShell Command: /help

Page 28: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 28

-> /list

1 : String foo = "Howdy"; 2 : long fibonacci(long n) { if (n == 0) return 0; if (n == 1) return 1; return fibonacci(n - 1) + fibonacci(n - 2); } 3 : interface Defloat { int convert(float x); } 4 : fibonacci(10) 5 : if ($4 > 20) printf("The value is %s\n", $4); 6 : import javax.swing.*; 7 : Defloat lamb = d -> Math.round(d); 8 : lamb.convert((float) 2.5)

JShell Command: /list

Page 29: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 29

JShell Commands: /vars, /methods, /classes

• Get a list of variables with /vars-> /vars| String foo = "Howdy"| long $4 = 55| Defloat lamb = $Lambda$1/1170794006@5ba23b66| int $8 = 3

• For information about methods there is /methods• For information about classes, interfaces, enums, and annotations

interfaces there is /classes

Page 30: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 30

-> /save mycode.jsh

-> /exit| Goodbye

% jshell | Welcome to JShell -- Version 1.9.0-internal| Type /help for help

-> /open mycode.jshThe value is 55

-> /list

1 : String foo = "Howdy";…

JShell Commands: /save, /open

Page 31: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 31

-> /list all

s1 : import java.util.*; s2 : import java.io.*; s3 : import java.math.*; s4 : import java.net.*; s5 : import java.util.concurrent.*; s6 : import java.util.prefs.*; s7 : import java.util.regex.*; s8 : void printf(String format, Object... args) { System.out.printf(format, args); }

Start-up File

Page 32: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 32

JShell Commands: start-up

• Use /setstart to set your own start-up• Use /savestart to get the current start-up• Both commands and snippets allowed in start-up• Too verbose? /feedback concise

Page 33: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 33

Command Line Editing

• Navigable history (up/down, enter)• Editing (emacs bindings)• Snippet and command completion (tab)• Argument type query (shift-tab)• Interruption (control-C)• Based on jline

Page 34: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 34

Agenda

What is JShell?

Why JShell?

Using JShell

Demo

Questions

1

2

3

4

5

Page 35: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 35

Demo

Page 36: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 36

JDK 9 / JShell Information

• Current schedule: GA September 22, 2016• Early access binaries + docs, updated weekly: https://jdk9.java.net/• Source code: http://hg.openjdk.java.net/jdk9/dev/ • -- Kulla (JShell) Project: http://openjdk.java.net/projects/kulla/• JEP 222: jshell: The Java Shell: http://openjdk.java.net/jeps/222

Page 37: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 37

API & Architecture• Tomorrow @ 16:10 • Interactive Java Support to your tool — the JShell API and Architecture

Page 38: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 38

The Team

• Engineering– Robert Field– Jan Lahoda

• OpenJDK Commiters– Shinya Yoshida– You?

• Testing– Andrei Eremeev

– Brian Goetz–Maurizio Cimadamore– Joe Darcy– Paul Sandoz– Jonathan Gibbons–Michel Trudeau

– Sundararajan Athijegannathan– Rémi Forax– Arun Gupta–Mani Sarkar– Daniel Daugherty

• Advisors / Cheerleaders / Reviewers

Page 39: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 39

Agenda

What is JShell?

Why JShell?

Using JShell

Demo

Questions

1

2

3

4

5

Page 40: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 40

Q & A

Page 41: JShell: An Interactive Shell for the Java Platform

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 41

Safe Harbor StatementThe preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 42: JShell: An Interactive Shell for the Java Platform