
Prototype your Java with JShell | Java for Beginners
Prototype your Java with JShell | Java for Beginners
Microsoft Principal Architect for Java and Java Champion Ed Burns introduces the Java Shell tool (JShell), an interactive command line tool for working with Java code.
Check out the rest of the Java for Beginners series at https://aka.ms/javaforbeginners
Samples and supporting materials can be found at https://aka.ms/JavaforBeginnersSamples
Recommended Microsoft Learn Modules: https://aka.ms/learnjavaonazure
Content
0 -> [MUSIC]
9.33 -> >> Greetings, everyone. Welcome
back to Java for Beginners.
13.185 -> I'm joined by Ed Burns,
15.45 -> who's going to talk
to us about JShell.
18.15 -> Ed, do you want to
introduce yourself
19.95 -> and tell us a little bit
about our topic today.
22.815 -> >> Thanks, Brian. I'm really
excited to be in the Java for
25.86 -> Beginners series and especially
talking about JShell,
29.43 -> which is a technology
that's really great
31.41 -> for beginners because it strips out
33.66 -> all of the other noise
that you don't have to
35.76 -> worry about with Java and lets
you focus just on the code.
38.475 -> It's even more lean than the leanest
IDE you can possibly imagine.
44.01 -> It's a read-evaluate-print
loop for Java.
46.56 -> It has full access to all of
48.12 -> the classes that you
would load and unload,
51.405 -> being able to access
the class loader in
54.54 -> a command line fashion
into a shell environment.
57.555 -> For developers that are
just coming to Java,
59.94 -> it's a great way to just
copy, paste, code in,
62.425 -> type it in, edit the code
and see what it does.
66 -> It's a very good beginner's tool.
69.74 -> In this brief video,
71.89 -> we're going to talk about JShell,
73.28 -> the shell for Java.
75.01 -> It's a command line tool
and it's a part of the JDK.
78.565 -> If you have Java 9 or newer,
80.62 -> you already have it installed.
82.925 -> It's useful because
it's a command line
86.37 -> read-evaluate-print loop for Java.
88.53 -> Think the approachability of
90.18 -> basic programming with
the power of Java.
93.15 -> This means it's a really great tool
94.98 -> for exploring new language features.
96.69 -> There's no compilation step,
98.43 -> no build process, no Maven or Ant.
100.92 -> You can really just get in and
get things started very quickly.
106.69 -> It was developed like
everything else in
109.37 -> core Java with community
governance and transparency.
112.79 -> If you want to see the original
Java enhancement process request,
116.57 -> you can follow this link here
and in the description below.
120.57 -> Everything done in the core JDK
is done with the JEP process.
124.915 -> Everything done in Java EE is
done with the JCP process.
129.49 -> A five minute video like
this is not intended to
132.07 -> teach everything there is
to know about anything.
134.925 -> But JShell on self has
136.57 -> very excellent context
aware in product help.
139.69 -> If you want to look for
the definitive guide,
141.7 -> you can follow the
documentation below.
144.065 -> Let's start it up. Here's a pro tip.
147 -> We are using -v printing arguments,
150.165 -> -v is for verbose feedback.
152.305 -> Printing is a shorthand
to cause JShell to import
155.5 -> the most common language features
156.91 -> related to printing on the console.
159.025 -> Let's take a look at that context
160.84 -> sensitive help that
I mentioned earlier.
162.775 -> Everything you type in JShell is
treated as a snippet of Java,
166.465 -> except if you start with a slash.
168.57 -> If you start with a slash,
170.05 -> you're talking to the
JShell interpreter.
172.105 -> These are called slash commands
and it's a tip of the hat
175.66 -> to the late 90s IRC text chat tool,
179.58 -> or as us olds like to call
it slack without emojis.
183.085 -> Here's another pro tip.
184.9 -> There is full tab completion.
186.49 -> Pressing the tab key will cause
the possible valid inputs,
189.4 -> giving the current
context to be presented,
192.04 -> prompting you to
choose the right one.
194.005 -> Let's talk about some
foundation concepts.
196.42 -> Aside from slash commands,
198.01 -> everything input into
JShell is a snippet.
200.5 -> There are three kinds of snippets.
203.23 -> Generally, you don't need
the closing semicolon.
205.58 -> JShell can usually tell when
207.01 -> one is required and
automatically add it.
209.23 -> A snippet is either an expression,
212.44 -> a statement, or a declaration.
214.57 -> Declarations can be
either non-nested.
217.305 -> That is top level declarations
that stand on their own or nested.
221.215 -> That is declarations that make
sense within other declarations.
224.725 -> I'll explain each one of these now.
227.285 -> Let's type an expression.
232.13 -> 2 plus 2 is an expression.
235.28 -> When you type an expression,
236.77 -> JShell will assign a
scratch variable to it,
239.83 -> so you can refer to it later.
241.81 -> You can see all of the currently
known variables by doing /vars.
249.88 -> You can see we have an assigned
252.52 -> $22 to the expression
we just typed 2 plus 2.
257.465 -> Let's talk about statements.
259.59 -> A statement is a Java statement,
262.54 -> just like what you'd expect.
264.175 -> Hello world is a statement.
266.44 -> You can say print "Hello world"
268.285 -> or if we're coming
from a C background,
270.775 -> we might say printf, percent
s, then exclamation point
274.44 -> "Hello world, " and it will
276.57 -> give us the same output with
the exclamation point there.
279.105 -> Those are statements.
280.98 -> Let's talk about declarations.
283.525 -> In Java, you declare something like
286.14 -> a class so you can use it later.
288.6 -> The complete list of declarations
in Java is import, method,
293.16 -> class, field, and interface.
296.91 -> First, let's review nested
versus non-nested declarations.
303.35 -> There are some rules for
305.89 -> non-nested declarations
which you can read here.
308.5 -> Generally these special
rules don't get in the way,
311.555 -> but they are necessary to allow
JShell to do its job where you're
317.18 -> basically typing in your statements
320.105 -> at the top level of the Java state.
324.17 -> Let's do an import.
326.14 -> The first kind of
declaration is an import.
328.6 -> We're going to import
scanner, java.util.Scanner.
332.425 -> If you do /imports,
you can see all of
335.05 -> the imports that we have
currently in the state.
337.87 -> The ones I've shown
above are imported by
340.72 -> either the -v printing or the
default load up startup ones.
348.7 -> That's imports.
350.785 -> Let's talk about methods.
352.81 -> Here's a simple method declaration.
355.405 -> This is a loan calculator.
357.25 -> These sample methods
and classes are part
359.65 -> of Java Champion and
educator Ken Fogel's
362.17 -> Java in Education Resources.
364.15 -> The link to these and other classes
is in the description below.
368.375 -> Let's go ahead and paste that in
and declare created method loan.
374.02 -> We can invoke the method just
like we would in Java by
376.87 -> doing a parentheses
after the method name.
379.75 -> We'll type in the term,
381.61 -> the interest rate and
the amount of the loan.
387.99 -> We see that our monthly
payment is 94.36.
392.41 -> If you'd like to see the method,
394.67 -> you can do /list,
396.43 -> and all of the things
that we've typed in so
398.96 -> far will be printed out.
401.9 -> You can also do /methods and see
the methods that we can call,
405.695 -> including the ones that were defined
by -printing. That's methods.
410.6 -> We can put the same
method inside of a class.
414.31 -> Here we have the same thing but
is inside of a class and we
417.65 -> call the loan method from
the main of that class.
421.295 -> We're going to declare
the class right there,
423.98 -> paste it in, or you could
type it if you like,
427.33 -> and now that it says created
class JavaCalculator01,
431.925 -> you can do /types
435.1 -> and see that we have
defined a new class there.
439.09 -> Let us invoke the loan method via
445.23 -> the main method and we're
just going to say new
448.47 -> JavaCalculator.main
and we're passing
451.48 -> the string array we need
for the main method,
453.73 -> so we get prompted for the
same set of values as before.
458.89 -> The next type of
declaration is a field.
461.86 -> Now, fields can be
defined at the top level,
464.38 -> the non-nested or, of course, as
467.065 -> member variables or static
variables inside of a class.
471.205 -> Now, we can see we've got
472.885 -> i as the one that we've
defined and also the
475.72 -> implicit one that we did with 2
plus 2 earlier, and that's fields.
481.04 -> Let's take a look at interfaces.
483.01 -> We will define a new
interface that we can
485.77 -> slap on to our existing
loan calculator class.
489.185 -> We'll define an
interface called Banker,
491.05 -> which has a method called loan,
492.55 -> which is a coincidence since
we've already defined loan.
496 -> Let us define the interface.
499.835 -> If we do types,
501.34 -> we can see we have interface
Banker and class JavaCalculator01.
505.645 -> If we do list,
507.175 -> we can see the class does
not implement the interface.
510.08 -> See, there's no implements
there, so how can we fix that?
513.205 -> Well, we can modify the state
516.085 -> and this is one of the really
impressive things about JShell.
520.45 -> All of the state is
completely mutable,
522.46 -> so we're going to edit our class
using tab completion there,
526.865 -> and we can see because I've
specified the editor value of Emacs,
531.65 -> it loads up Emacs and loads the
class there and we're going
536.48 -> to specify implements
Banker and save the file.
542.71 -> If we do /list again,
546.325 -> you'll see that it now
implements Banker.
550.395 -> That's the intro to JShell.
552.56 -> Stay tuned for the second part where
554.59 -> I show a practical application
556.72 -> of interacting with a Java EE plain
old Java object using JShell.
562.95 -> Thanks for watching.
Please take a look
565.72 -> at the resources for JShell in
567.61 -> the show notes and enjoy many of
570.04 -> the other episodes we have in
the Java for Beginners series.
573.55 -> >> Thank you, Ed.
574.78 -> [MUSIC]
Source: https://www.youtube.com/watch?v=201IpTSRIGs