BeanShell is a scripting language based on Java which looks a lot like Java. It is much friendlier for beginners than Java is. If you learn BeanShell then learning Java will be much easier.
Download bsh-xx.jar and put it in your working directory.
~/beanshell $ ls -F
bsh-2.0b4.jar bsh_console.sh*
~/beanshell $ cat bsh_console.sh
#!/bin/bash
export CLASSPATH=bsh-2.0b4.jar
java bsh.Console &
~/beanshell $
Copy my bsh_console.sh
into your working directory. A jar
file contains compiled Java code. The CLASSPATH
tells the java
command where to find compiled Java code.
Now you can run BeanShell:
~/beanshell $ ./bsh_console.sh
~/beanshell $
You should see a new window that lets you interact with BeanShell. From now on I will mostly refer interactions in that window.
From the window:
bsh % pwd();
/Users/fschmidt/beanshell
bsh % print("Hello world!");
Hello world!
bsh % print(1+1);
2
bsh %
The BeanShell pwd
command is like the Bash pwd
command, and the BeanShell print
command is somewhat like the Bash echo
command. But the syntax is different. BeanShell and Java have syntax like most programming languages. While Bash does command arg1 arg2 arg3
, most programming languages do command(arg1,arg2,arg3)
. In addition, Java requires a ;
at the end of a statement (command line).
pwd
and print
are only BeanShell commands. They are not in Java.
Also note that Java has the standard arithmetic operators.
bsh % String message = "Hello world!";
bsh % print(message);
Hello world!
bsh % int i = 2;
bsh % print(i);
2
bsh % print(i+1);
3
bsh % i = i + 5;
bsh % print(i);
7
bsh %
Variables hold values. In Java (but not BeanShell), variables must be declared with a type. The type String
is for text, and int
is for integers.
The =
command assigns a value to a variable.
bsh % print( message.toUpperCase() );
HELLO WORLD!
bsh % print( message.length() );
12
bsh % print( message.getClass() );
class java.lang.String
bsh %
Java is an object-oriented language which means that it mostly deals with objects. I will explain with real-world examples. To show a dog or a toaster, I would do something like show(dog)
or show(toaster)
. Showing is something I do to these objects, it is not built into the objects. In contrast, to have a dog run I would do dog.run()
, and to have a toaster toast bread I would do toaster.toast(bread)
. Running is a function built into dogs, while toasting is built into toasters. Functions that are built into objects are called "member functions".
But actually this goes deeper. Dogs are a type of canine and all canines can run. So the ability of dogs to run actually comes from them being canines. In Java, we would call dogs, canines, and toasters "classes", and we would say that dogs are a subclass of canines, and that canines are a superclass of dogs.
Back to programming. The class of message
(a String) is java.lang.String. Click that link and see all the things that strings can do. For example, they can toUpperCase and length. These functions return values. Strings can also getClass but this functionality comes from the superclass java.lang.Object. Objects are the base class of everything. Look again at the top of java.lang.String and you will see the class hierarchy and see that String is a subclass of Object.
bsh % List list = new ArrayList();
bsh % print( list );
[]
bsh % print( list.size() );
0
bsh % list.add("a");
bsh % print( list );
[a]
bsh % print( list.size() );
1
bsh % list.add("b");
bsh % list.add("c");
bsh % print( list );
[a, b, c]
bsh % print( list.size() );
3
bsh % print( list.get(0) );
a
bsh % print( list.get(1) );
b
bsh % print( list.getClass() );
class java.util.ArrayList
bsh %
java.util.ArrayList is a class. To make a new object of a class, we use the new
operator. If you look at the top of the ArrayList page, you will see "All Implemented Interfaces:" which includes "List" and clicking that takes you to java.util.List. List is an interface which you can think of as a purely abstract class. An interface only specifies what an object does, not how. A concrete class like ArrayList specifies how objects do what they do (the implementation) so that the object can actually do these actions. Note that an interface can be implemented/subclassed by multiple classes that implement the interface in different ways. You can see this in java.util.List under "All Known Implementing Classes:".
Note that the first index of a list is 0 not 1. So the last index of a list is list.size() - 1
.
Java organizes classes into packages. The package of List and ArrayList is java.util. It is full of useful classes. Java has a huge number of standard classes, and I am not going to go over any more classes than needed to convey the core concepts of Java. You can learn the classes you need on your own.
bsh % print( 3 > 4 );
false
bsh % print( 3 >= 4 );
false
bsh % print( 3 < 4 );
true
bsh % print( 3 <= 4 );
true
bsh % print( 3 == 4 );
false
bsh % print( 3 != 4 );
true
bsh % print( 3 < 4 && 3 > 4 );
false
bsh % print( 3 < 4 || 3 > 4 );
true
bsh % boolean b = 3 < 4;
bsh % print(b);
true
bsh % print( b == true );
true
bsh %
==
checks for equality, !=
checks for inequality, &&
is for "and", and ||
is for "or". These are boolean operators that return boolean values, unlike something like +
which is an arithmetic operator. boolean
is a type than can have the values true
or false
.
bsh % String s1 = new String("hi");
bsh % String s2 = new String("hi");
bsh % print( s1 == s2 );
false
bsh % print( s1.equals(s2) );
true
bsh % String s3 = s1;
bsh % print( s1 == s3 );
true
bsh %
If you have two objects in the physical world that are identical, then they are not the same object but they are equivalent objects. In Java, object1 == object2
checks whether object1 and object2 refer to the same object. Object variables refer to objects, they don't contain the object's value. object1.equals(object2)
checks for object equivalence.
The types int
and boolean
are primitive types. They are not objects. Primitive type variables contain primitive values, so they don't refer to anything. So in this case, ==
checks for equal values.
Now I will return to the Bash command line.
~/beanshell $ ls -F
bsh-2.0b4.jar bsh.sh* bsh_console.sh* code.bsh
~/beanshell $ cat bsh.sh
#!/bin/bash
export CLASSPATH=bsh-2.0b4.jar
java bsh.Interpreter $*
~/beanshell $ cat code.bsh
/* This is a comment */
// This is also a comment
print("Hi");
print("Bye");
~/beanshell $ ./bsh.sh code.bsh
Hi
Bye
~/beanshell $
bsh.sh
runs a BeanShell file from the command line. I will keep modifying code.bsh
to show my examples.
In Java, comments are between /*
and */
or between //
and the end of the line. Comments are ignored.
~/beanshell $ cat code.bsh
int x = 5;
print("first if");
if( x == 5 ) {
print("x == 5");
}
print("second if");
if( x > 5 ) {
print("x > 5");
} else {
print("x <= 5");
}
print("third if");
if( x > 5 ) {
print("x > 5");
} else if( x < 5 ) {
print("x < 5");
} else {
print("x == 5");
}
~/beanshell $ ./bsh.sh code.bsh
first if
x == 5
second if
x <= 5
third if
x == 5
~/beanshell $
if
statements take a boolean argument.
~/beanshell $ cat code.bsh
~/beanshell $ cat code.bsh
print("first loop");
int i = 1;
while( i <= 3 ) {
print(i);
i = i + 1;
}
print("second loop");
for( int i = 1; i <= 3; i = i + 1 ) {
print(i);
}
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
print("third loop");
for( int i = 0; i < list.size(); i = i + 1 ) {
String s = list.get(i);
print(s);
}
print("fourth loop");
Iterator iter = list.iterator();
while( iter.hasNext() ) {
String s = iter.next();
print(s);
}
print("fifth loop");
for( String s : list ) {
print(s);
}
~/beanshell $ ./bsh.sh code.bsh
first loop
1
2
3
second loop
1
2
3
third loop
a
b
c
fourth loop
a
b
c
fifth loop
a
b
c
~/beanshell $
The first form of the for
loop has three elements: an initialization statement, a loop condition, and a statement that is run after each iteration of the loop. The second form of the for
loop iterates through anything that is iterable, returning values in sequence. For the fourth loop, see List.iterator().
~/beanshell $ cat code.bsh
{
String s = "ok";
print( "first " + s );
}
print( "second " + s );
~/beanshell $ ./bsh.sh code.bsh
first ok
second void
~/beanshell $
Code between the braces {
and }
is a block. When braces are part of a statement like a for
statement, the block extends to include the whole statement. Variables declared inside a block only exist within that block.
In BeanShell an undefined variable is void
but in Java you would get a compile error.
Note that besides being an arithmetic operator, +
also concatenates strings.
~/beanshell $ cat code.bsh
String[] array = { "a", "b", "c", "d", "e" };
print( "array = " + Arrays.asList(array) );
print( "length = " + array.length );
print( "array[1] = " + array[1] );
array[1] = "bb";
print( "modified array = " + Arrays.asList(array) );
print("loop");
for( String s : array ) {
print(s);
}
array = new String[3];
print( "new array = " + Arrays.asList(array) );
array[2] = "two";
print( "modified new array = " + Arrays.asList(array) );
~/beanshell $ ./bsh.sh code.bsh
array = [a, b, c, d, e]
length = 5
array[1] = b
modified array = [a, bb, c, d, e]
loop
a
bb
c
d
e
new array = [null, null, null]
modified new array = [null, null, two]
~/beanshell $
Arrays are an anomaly - neither primitive type nor object. Arrays don't do anything that lists can't do. The reason that Java has arrays is because arrays are a fundamental low-level programming concept, and Java is a low-level language.
Arrays are declared with the type that they will hold, and then they can only hold that type. Arrays cannot change size.
Since arrays don't know how become strings, I used Arrays.asList() which converts an array into a list so that it can be displayed properly. Note that this is a static
function which means that it isn't part of an object, but rather is a general function that is associated with a class.
In the second part of the code, I assign array
to a new array of length 3 with no values. Java uses the special value null
to mean no value.
~/beanshell $ cat code.bsh
int add(int x,int y) {
return x + y;
}
void addAndShow(int x,int y) {
int sum = x + y;
print( x + " + " + y + " = " + sum );
}
void sayBye() {
print("Bye");
}
print( "add(2,3) = " + add(2,3) );
addAndShow(2,3);
sayBye();
~/beanshell $ ./bsh.sh code.bsh
add(2,3) = 5
2 + 3 = 5
Bye
~/beanshell $
Functions make possible reusable code, and are the core concept of programming. In Java, function definitions start with the return type, then the function name, then the function parameters with types, and then a block that implements the function. A return type of void
means nothing is returned.
~/beanshell $ cat code.bsh
Iterator iter = new Iterator() {
int i = 0;
boolean hasNext() {
return i < 10;
}
int next() {
i = i + 1;
return i;
}
};
while( iter.hasNext() ) {
print( iter.next() );
}
~/beanshell $ ./bsh.sh code.bsh
1
2
3
4
5
6
7
8
9
10
~/beanshell $
BeanShell doesn't let you implement new classes, you must use Java for that. But BeanShell does let you implement interfaces which is similar to implementing a new class. In this case, I implement Iterator which defines two member functions which must be implemented. Typically an implementation will include both member functions and member data/variables. The member data describes the state of the object, and the member functions often modify that state. This iterator counts from 1 to 10, and the member variable i
keeps track of the current count.
This example is just to give a taste of object-oriented programming. When you learn Java, you will learn much more about this.
The purpose of this tutorial is to introduce basic Java concepts that will make learning Java easier. I avoided details and involved examples.
When you are learning Java, I still think BeanShell is a useful tool for experimentation. You can quickly test things in BeanShell that would take longer to test in Java.