1
00:00:00,005 --> 00:00:04,008
- [Narrator] One of the
JEPs; JEP 222 is enhancement

2
00:00:04,008 --> 00:00:09,002
called jshell; let's see what that means.

3
00:00:09,002 --> 00:00:13,002
So, JEP222 - jshell is the Java Shell.

4
00:00:13,002 --> 00:00:16,009
The REPL; The Read-Evaluate-Print Loop.

5
00:00:16,009 --> 00:00:19,008
This new tool allows us to prototype

6
00:00:19,008 --> 00:00:22,007
and explore coding options
with immediate results

7
00:00:22,007 --> 00:00:24,007
and feedback, which is really nice

8
00:00:24,007 --> 00:00:28,000
and makes Java interactive
from the command line.

9
00:00:28,000 --> 00:00:31,000
This tool can be used for
evaluating declarations,

10
00:00:31,000 --> 00:00:33,005
statements and expressions in Java.

11
00:00:33,005 --> 00:00:36,003
We can even make little mini methods.

12
00:00:36,003 --> 00:00:38,007
Let's go ahead and switch
over to a command prompt

13
00:00:38,007 --> 00:00:42,000
and I'll show you how you can run jshell.

14
00:00:42,000 --> 00:00:45,001
I'm using a Windows machine,
so I have the command prompt

15
00:00:45,001 --> 00:00:48,007
up and notice, I'm
already in jdk-9 folder,

16
00:00:48,007 --> 00:00:50,009
so you want to make sure
you navigate to there

17
00:00:50,009 --> 00:00:54,002
and I'm going to type in
jshell, but I wanted to use

18
00:00:54,002 --> 00:00:56,005
the Verbose addition and
that way we can kind of

19
00:00:56,005 --> 00:01:00,008
get more information as we
use some of the commands.

20
00:01:00,008 --> 00:01:01,008
Once you get used to it, you might

21
00:01:01,008 --> 00:01:05,005
not want to do that,
but I'm going to add -v.

22
00:01:05,005 --> 00:01:06,009
It says that it's not recognized.

23
00:01:06,009 --> 00:01:09,009
I forgot; jshell is
actually in the Bin folder,

24
00:01:09,009 --> 00:01:12,002
so I need to change this; I
need to go one more level.

25
00:01:12,002 --> 00:01:16,000
I'm going to do cd bin; if
I hit the up arrow twice,

26
00:01:16,000 --> 00:01:19,004
it gives me the command jshell -v

27
00:01:19,004 --> 00:01:22,003
and now I can hit Enter.

28
00:01:22,003 --> 00:01:24,009
Alright, so it says Welcome
to jshell version nine.

29
00:01:24,009 --> 00:01:27,007
For an introduction, type /help intro.

30
00:01:27,007 --> 00:01:29,008
Well, let's try that, so I'm going to do

31
00:01:29,008 --> 00:01:33,001
/help intro and you can see here.

32
00:01:33,001 --> 00:01:35,001
Let me scroll down a little bit.

33
00:01:35,001 --> 00:01:37,001
It says the jshell tool allows you

34
00:01:37,001 --> 00:01:40,004
to execute Java code and
get immediate results.

35
00:01:40,004 --> 00:01:41,005
Awesome.

36
00:01:41,005 --> 00:01:43,003
You can read the rest of that yourself

37
00:01:43,003 --> 00:01:44,008
and at the very bottom, now it says

38
00:01:44,008 --> 00:01:47,001
for a list of commands, use /help.

39
00:01:47,001 --> 00:01:48,008
Well, let's check that out, as well.

40
00:01:48,008 --> 00:01:50,009
So, let's do /help and you can see

41
00:01:50,009 --> 00:01:53,003
here are all the different commands.

42
00:01:53,003 --> 00:01:54,006
One of the most important ones

43
00:01:54,006 --> 00:01:56,006
that I'll point out now,
because I don't want

44
00:01:56,006 --> 00:01:59,007
you to struggle is the
/exit, cause sometimes

45
00:01:59,007 --> 00:02:02,006
it's hard to figure out how
to get out of the jshell.

46
00:02:02,006 --> 00:02:06,005
If you're done, you want
to get out; you do /exit.

47
00:02:06,005 --> 00:02:07,008
Alright, I'll scroll up little further,

48
00:02:07,008 --> 00:02:10,007
just so you can see all the
different types of commands.

49
00:02:10,007 --> 00:02:15,001
/list, /edit, /drop.

50
00:02:15,001 --> 00:02:16,009
Alright, well, now that
we've seen the help,

51
00:02:16,009 --> 00:02:19,006
let's go ahead and try a few things.

52
00:02:19,006 --> 00:02:21,007
So, what I'm going to do is I am going to

53
00:02:21,007 --> 00:02:23,008
go ahead and just start
with something simple.

54
00:02:23,008 --> 00:02:26,003
Let's see what happens
if I put in a number.

55
00:02:26,003 --> 00:02:28,006
Let's do three.

56
00:02:28,006 --> 00:02:30,002
If you just put a number in, it's going to

57
00:02:30,002 --> 00:02:33,003
automatically create what
they call a scratch variable

58
00:02:33,003 --> 00:02:35,003
and since it is a first
variable that I created,

59
00:02:35,003 --> 00:02:36,008
it gave it a number one.

60
00:02:36,008 --> 00:02:39,001
$1 is an integer.

61
00:02:39,001 --> 00:02:42,001
And so, now, if I type $1, it'll show me

62
00:02:42,001 --> 00:02:45,003
the contents of that variable
and the contents here.

63
00:02:45,003 --> 00:02:48,001
It's an integer and it
has a value of three.

64
00:02:48,001 --> 00:02:49,007
If I wanted to create another one,

65
00:02:49,007 --> 00:02:51,008
I just type in a four and you'll see

66
00:02:51,008 --> 00:02:55,006
now it gave me $3 has the value four,

67
00:02:55,006 --> 00:02:57,005
so now I have two variables.

68
00:02:57,005 --> 00:03:00,002
I have $1 and $3.

69
00:03:00,002 --> 00:03:01,007
Normally, you won't actually allow it

70
00:03:01,007 --> 00:03:04,003
to create the variables; usually,
you're creating yourself.

71
00:03:04,003 --> 00:03:07,009
Let's do int x = 5; and you can see here,

72
00:03:07,009 --> 00:03:11,002
I created a variable
x and it's an integer.

73
00:03:11,002 --> 00:03:12,005
Now, one of the really nice things

74
00:03:12,005 --> 00:03:14,008
is right now, I don't have
a whole lot of variables

75
00:03:14,008 --> 00:03:17,008
created, but if I had
more, at any point in time,

76
00:03:17,008 --> 00:03:20,007
you can do /list, and it'll show you

77
00:03:20,007 --> 00:03:23,002
what each of the variables has in it.

78
00:03:23,002 --> 00:03:25,009
Okay, you can see here,
we created four variables.

79
00:03:25,009 --> 00:03:27,008
The first one has the value of three.

80
00:03:27,008 --> 00:03:31,004
The second one actually has
the value of the first one.

81
00:03:31,004 --> 00:03:35,000
The $1 and then we have the
third one; has a value of four

82
00:03:35,000 --> 00:03:37,003
and then the fourth one has the value

83
00:03:37,003 --> 00:03:40,003
that says x = 5, so that's kind of handy

84
00:03:40,003 --> 00:03:42,004
if you have a lot of stuff you're doing;

85
00:03:42,004 --> 00:03:45,001
you want to see exactly where you're at.

86
00:03:45,001 --> 00:03:47,002
Let's go ahead and add some value,

87
00:03:47,002 --> 00:03:48,007
so let's say what happens when I do

88
00:03:48,007 --> 00:03:51,005
x + 10; again that's a statement.

89
00:03:51,005 --> 00:03:54,004
Now, notice, I don't
have to put a semicolon.

90
00:03:54,004 --> 00:03:58,003
It's optional and this
gave me the value of 15.

91
00:03:58,003 --> 00:04:04,008
I could even say maybe int y = x + 5

92
00:04:04,008 --> 00:04:08,000
and now y has the value of 10,

93
00:04:08,000 --> 00:04:10,001
so you can see; you can create variables.

94
00:04:10,001 --> 00:04:12,008
You can use the regular math symbols.

95
00:04:12,008 --> 00:04:16,006
You can multiply, divide,
even find remainders.

96
00:04:16,006 --> 00:04:17,008
Maybe you want to see what happens

97
00:04:17,008 --> 00:04:21,001
if I divide two integers,
like one divided by two.

98
00:04:21,001 --> 00:04:23,004
If you're familiar with Java, you know

99
00:04:23,004 --> 00:04:25,006
that two integers does not give you

100
00:04:25,006 --> 00:04:27,007
any decimal portion of the number,

101
00:04:27,007 --> 00:04:30,006
so it created a variable
with the value zero.

102
00:04:30,006 --> 00:04:33,001
I could fix it by changing
one of my variables

103
00:04:33,001 --> 00:04:39,002
to have a decimal and now,
I'll get the 0.5 as the answer.

104
00:04:39,002 --> 00:04:40,004
The last thing I wanted to show

105
00:04:40,004 --> 00:04:43,001
is you can even create your
own methods, so let's do that.

106
00:04:43,001 --> 00:04:44,005
Let's create a little method.

107
00:04:44,005 --> 00:04:46,009
We always do Hello World, so let's do

108
00:04:46,009 --> 00:04:49,009
Hello jshell, just that's
pretty familiar to everybody.

109
00:04:49,009 --> 00:04:51,002
So, I'm going to make my method.

110
00:04:51,002 --> 00:04:55,007
I'm going to type void helloJShell

111
00:04:55,007 --> 00:04:58,000
and its a method that takes in an integer

112
00:04:58,000 --> 00:05:00,001
and the integer will
tell me how many times

113
00:05:00,001 --> 00:05:03,007
I'm supposed to loop through
and print the jshell.

114
00:05:03,007 --> 00:05:05,005
I'm going to do an open curly bracket.

115
00:05:05,005 --> 00:05:09,006
Now, I can hit Enter and
since the jshell Editor

116
00:05:09,006 --> 00:05:12,001
knows that the open
curly bracket is not done

117
00:05:12,001 --> 00:05:14,000
until it reaches a closed curly bracket,

118
00:05:14,000 --> 00:05:16,009
it knows that I want to
continue to the next line,

119
00:05:16,009 --> 00:05:19,007
so in the next line, I'm
going to add a little for loop

120
00:05:19,007 --> 00:05:22,005
and I'll just say for index equals zero

121
00:05:22,005 --> 00:05:26,000
as long as x is less than
i which is my variable

122
00:05:26,000 --> 00:05:28,001
coming in from the calling program

123
00:05:28,001 --> 00:05:32,005
and I'm going to say x++
and what I want to happen

124
00:05:32,005 --> 00:05:34,009
is every time it goes through the loop,

125
00:05:34,009 --> 00:05:40,009
I want it to print out, so I'm
going to do System.out.printline

126
00:05:40,009 --> 00:05:45,008
and I'm going to print out hello jshell.

127
00:05:45,008 --> 00:05:47,006
Okay, now that I have that set up,

128
00:05:47,006 --> 00:05:49,008
if I hit Enter, I'm still continuing,

129
00:05:49,008 --> 00:05:52,002
cause I haven't closed this method yet,

130
00:05:52,002 --> 00:05:55,002
so let's close the method
with the closed curly bracket

131
00:05:55,002 --> 00:05:57,007
and now, when we hit Enter,
it tells me that I created

132
00:05:57,007 --> 00:06:01,001
a method called hello
shell that takes an integer

133
00:06:01,001 --> 00:06:04,007
and now, we can run
(mumbles) method; helloJShell

134
00:06:04,007 --> 00:06:08,005
and let's give it a number
five and you can see

135
00:06:08,005 --> 00:06:12,006
it printed out the words
hello jshell five times.

136
00:06:12,006 --> 00:06:16,006
Okay, now I scrolled up, so I
have hello jshell at the top.

137
00:06:16,006 --> 00:06:20,002
Another nice feature
within the jshell tool

138
00:06:20,002 --> 00:06:22,007
is the ability to use the tab completion,

139
00:06:22,007 --> 00:06:25,002
so let's say, I started to type in hello,

140
00:06:25,002 --> 00:06:28,003
I can click on the TAB key and it says:

141
00:06:28,003 --> 00:06:30,002
Oh, did you want to do hello shell?

142
00:06:30,002 --> 00:06:31,000
And I did.

143
00:06:31,000 --> 00:06:32,007
And we'll put a three in there this time

144
00:06:32,007 --> 00:06:35,002
and I'll do a closed
parenthesis and hit Enter

145
00:06:35,002 --> 00:06:37,008
and now, it says hello shell three times,

146
00:06:37,008 --> 00:06:41,003
so the tab completion
is a very nice feature.

147
00:06:41,003 --> 00:06:43,001
One other things that I wanted to show you

148
00:06:43,001 --> 00:06:45,005
was you probably want to check and see

149
00:06:45,005 --> 00:06:47,006
what are all the different libraries

150
00:06:47,006 --> 00:06:51,000
available to me in jshell
and I want you to know,

151
00:06:51,000 --> 00:06:53,007
you could also import you
own, but let's go ahead

152
00:06:53,007 --> 00:06:57,003
and type /imports; there, you can see

153
00:06:57,003 --> 00:07:00,007
that it has the io library;
it has the math library;

154
00:07:00,007 --> 00:07:04,004
it has the net library, nio, the util

155
00:07:04,004 --> 00:07:08,002
and then several of the
util specific libraries.

156
00:07:08,002 --> 00:07:10,007
So, a couple of things to
remember regarding jshell.

157
00:07:10,007 --> 00:07:12,003
One is that you in order to launch it,

158
00:07:12,003 --> 00:07:16,001
you want to be in the Bin
folder of your jdk-9 folder

159
00:07:16,001 --> 00:07:20,000
and when you want to get out,
you're going to type /exit.

160
00:07:20,000 --> 00:07:22,002
Have fun with this new tool.

