Skip to content

Hello, World!! JavaShell Introduction

Ultimate Pea edited this page Feb 8, 2017 · 7 revisions

##This guide is written on Release 0.1.x

#This is a Hello, World guide on JavaShell.

##1. Setup Development Environment After following install instructions, you should have four class files in your directory. If not, compile the source.

$ javac *.java

##2. Boot the shell

$ java JavaShell

There should be a welcoming message.

##3. Load a class Objects are the building blocks of this shell script. Before execute any method, make sure that there is at least one working instance.

We are going to play around Integer class first.

>>> load java.lang.Integer 12

JavaShell will attempt to construct an Integer object with the constructor that takes String 12 as argument. You should see a message that says the construction is successful. Also notice that the command prompt changes the current operating instance's class name.

##4. See what actions are available Type ls followed by a space at the prompt.

java.lang.Integer >>> ls

ls will list all the methods with their arguments at the command line prompt. You can call any method listed with apporiate argument.

##5. Call a method on a java object To call a method with no argument, simply type the method's name followed by a space. For example, if we were to call this Integer's toString method, we simply type

java.lang.Integer >>> toString

As you can see, the return value is 12, which is the return value of calling toString() on a Integer.


In fact, what the two lines really do is just a Java Statement.

load java.lang.Integer 12
toString

is the same as

new Integer("12").toString();

##6. Capture the return variables JavaShell keeps a global hashtable of all references it creates. You can store a variable (bind) by its name, retrieve a variable by its name (unbind), and show what is in the bindings (showbind).

Try typing the showbind command.

java.lang.Integer >>> showbind

You should see the rtObj bindings, which captures the return value of previous executed method. (toString() returns a string of the currently operation instance, which is 12).

Now let's try the sum method in the Integer class, which sums two integers.

java.lang.Integer >>> sum 3 5

You should see the return value is 8. Type showbind again will show that the rtObj has changed.

java.lang.Integer >>> showbind

##7. Use the bindings as arguments

Bindings can be directly used as arguments, when a name is used in place of an argument, JavaShell will look for the bindings before it tries to construct the argument using a contructor.

For example, if we want to use rtObj as a argument in our subsequent method call, we can directly use rtObj in place of the argument.

java.lang.Integer >>> sum rtObj 6

The above command will sum 8 which is the rtObj in the bindings, with 6 to get 14.

java.lang.Integer >>> showbind

The showbind command reveals that the return value now changed to 14.

##8. Store objects

Before we proceed, we can store our current operating Integer 12 in the bindings table using bind command, followed by a name.

java.lang.Integer >>> bind twelve
java.lang.Integer >>> showbind

bind twelve will bind currently operating Instance -- 12, not the rtObj, which is already a binding, to a name "twelve". You can see that the bindings table contains one more entry, namely twelve : 12.

####Notice that the rtObj does not change, this is true. Only method calls on instance changes the rtObj. load ls bind unbind showbind are the only shell actions that do not change the rtObj. These actions also don't show up in the ls commands list.

##9. Get the object in the bindings

What if we want to operate on the object stored in the bindings table? We can set the current operating object to be any object in the bindings table by the unbind command.

java.lang.Integer >>> unbind rtObj
java.lang.Integer >>> toString

The current operation object changes to Integer 14 instead of twelve. We can actually compare the equality of the two object by using equals method of the Integer instance.

java.lang.Integer >>> equals twelve
java.lang.Integer >>> compareTo twelve

#Summary

That is the basic command in the JavaShell. Below is a summary of this wiki.

  1. load <classname> <constructor_parameter> to load an object to be currently operating object.
  2. ls to view available methods to call on this object
  3. <method_name> <param1> <param2> calls the method on this object
  4. rtObj stores the method return value, load ls bind unbind showbind does not change rtObj
  5. bind <name> stores the current operating object
  6. unbind <name> retrieves the stored object and changes currently operating object
  7. showbind shows a lit of bindings`

###When you are ready, please move to Part 2

Clone this wiki locally