By Chiccodb00 https://www.youtube.com/channel/UCtF8xxYtL6cVSVP2wkMj1Sg
Hi, I'm Chiccodb00
in this video I'll explain the basic usage
and JShell functions.
JShell, is one of the novelties introduced with Java 9
which I have already discussed in the video
available at the top right.
In order to run it on Linux,
just install Java 9 and run the "jshell" command
for Windows, you must always have Java 9 installed
but trying to typing jshell on the terminal we would get
an error message that warns us that the jshell command
isn't recognized, in fact you have to add the JDK9
installation folder to the system path
to do this, simultaneously press the Windows key and "R"
and type "control panel" followed by enter
from here we go to "System and Security"
"System", "Advanced System Settings"
"Environment variables".
At this point we go under "System Variables"
select "Path" and "Edit ..."
at this point you can see two different windows
based on the Windows version, I will do the procedure
both at the same time.
If you are presented with a screen similar to the one on the left
click on "New" and write "C:\Program Files\Java\jdk-9\bin"
finally click on "OK".
If you are presented with a screen similar to the one on the right
go to the end of the "Variable Value" text box
and add ";C:\Program Files\Java\jdk-9\bin;",
finally click on "OK"
on the screen containing all the environment variables
click again on "OK" and close all other windows.
Now we're ready to use JShell
to do that, simultaneously press the Windows and R key
write "jshell" and press enter.
After a few moments there will be a welcome message,
then follow the instructions by typing "/help intro"
in the message that appears you'll be told that JShell
is a tool that allows us to run code in Java
and get instant results
that we can declare methods, variables etc ...
and we can also perform expressions like x + x.
I would begin by declaring an integer called t with value 5
then I write "int t = 5" (here doesn't even need the semicolons)
and pressing enter will show "t ==> 5",
Now we can perform some operations on this variable
such as printing numbers from 1 to t using a for loop
then write "for (int i = 0; i <t; i ++)"
this time hitting enter will not return any output
as it is waiting for the instructions to be executed within the for
then write "System.out.println(i+1)"
pressing enter will run the cycle,
because as a result will give us 1, 2, 3, 4, 5.
You can notice how JShell can be used
also as a calculator, for example, writing "4*4"
and hitting enter will return us 16.
Now let's go to JShell's actual commands
let's start by /list that allows us to print
the "historical" of all the commands we have performed,
in our case it will return:
1: int t = 5;
2: for(int i = 0; i<t; i++) System.out.println(i+1);
3: 4*4
The /list command can also be used to get other informations
for example writing /list and the id of an instruction,
it will return the instruction
if we write /list -start, we will see all the imported classes,
while if we write /list -all we will get both the imported classes
both the instructions executed.
We can also save our hard work
using the /save command followed by the name of the file
then reopen it by writing /open and the filename
note that the /open command will also execute the entire snippet.
Before you save the file you may want to remove an instruction
that you do not want it to appear,
to do that we write /drop and the id of an instruction,
it is obtained when the instruction /list is executed,
since it is the number before ":"
for example we could remove the 3rd statement with /drop 3,
if we want to change an instruction
just write /edit and the instruction id
for example we can make sure that instead
printing the numbers starting from 1,
print them from 0, to do that we write /edit 2,
at this point it will open a window called "JShell Edit Pad"
which contains the current instruction,
and in our case also the condition,
then within the same we modify i + 1
with i, click on "Accept" and "Exit".
It should be noted that in case of a statement
it will be "moved" at the bottom of the history,
and in the case of a for, a copy will be created at the end.
We can see the declared variables and their values
through the /vars command and the methods with their signatures with /methods.
You can re-run a specified snippet
inside an id writing / and the id
Finally, you can reset the Jshell
via the /reset command.
I have omitted some commands,
which are available by writing /help.
If the video helped you understand the subject,
leave a like,
otherwise leave a dislike.
Anyway, you can always comment to help me improve my future videos!
No comments:
Post a Comment