Mercurial Hosting > reactionary
changeset 88:f320d444face default tip
finish beanshell
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 22 Apr 2025 23:11:59 -0600 |
parents | 5f4cc9d3d3cb |
children | |
files | src/beanshell.html.luan |
diffstat | 1 files changed, 369 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/src/beanshell.html.luan Mon Apr 21 21:47:15 2025 -0600 +++ b/src/beanshell.html.luan Tue Apr 22 23:11:59 2025 -0600 @@ -25,8 +25,8 @@ <code block> ~/beanshell $ ls -F -bsh-2.0b4.jar bsh.sh* -~/beanshell $ cat bsh.sh +bsh-2.0b4.jar bsh_console.sh* +~/beanshell $ cat bsh_console.sh #!/bin/bash export CLASSPATH=bsh-2.0b4.jar @@ -34,12 +34,12 @@ ~/beanshell $ </code> -<p>Copy my <code>bsh.sh</code> into your working directory. A <code>jar</code> file contains compiled Java code. The <code>CLASSPATH</code> tells the <code>java</code> command where to find compiled Java code.</p> +<p>Copy my <code>bsh_console.sh</code> into your working directory. A <code>jar</code> file contains compiled Java code. The <code>CLASSPATH</code> tells the <code>java</code> command where to find compiled Java code.</p> <p>Now you can run BeanShell:</p> <code block> -~/beanshell $ ./bsh.sh +~/beanshell $ ./bsh_console.sh ~/beanshell $ </code> @@ -113,7 +113,7 @@ bsh % </code> -<p>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 <code>show(dog)</code> or <code>show(toaster)</code>. 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 <code>dog.run()</code>, and to have a toaster toast bread I would do <code>toaster.toast(bread)</code>. Running is a function built into dogs, while toasting is built into toasters.</p> +<p>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 <code>show(dog)</code> or <code>show(toaster)</code>. 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 <code>dog.run()</code>, and to have a toaster toast bread I would do <code>toaster.toast(bread)</code>. Running is a function built into dogs, while toasting is built into toasters. Functions that are built into objects are called "member functions".</p> <p>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.</p> @@ -153,7 +153,7 @@ bsh % </code> -<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html">java.util.ArrayList</a> is a class. To make a new object of a class, we use the <code>new</code> 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 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html">java.util.List</a>. 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 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html">java.util.List</a> under "All Known Implementing Classes:".</p> +<p><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html">java.util.ArrayList</a> is a class. To make a new object of a class, we use the <code>new</code> 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 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html">java.util.List</a>. 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 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html">java.util.List</a> under "All Known Implementing Classes:".</p> <p>Note that the first index of a list is 0 not 1. So the last index of a list is <code>list.size() - 1</code>.</p> @@ -163,6 +163,369 @@ <% end } + boolean = { + title = [[Boolean Values and Equality]] + content = function() +%> + +<code block> +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 % +</code> + +<p><code>==</code> checks for equality, <code>!=</code> checks for inequality, <code>&&</code> is for "and", and <code>||</code> is for "or". These are boolean operators that return boolean values, unlike something like <code>+</code> which is an arithmetic operator. <code>boolean</code> is a type than can have the values <code>true</code> or <code>false</code>.</p> + +<code block> +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 % +</code> + +<p>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, <code>object1 == object2</code> checks whether object1 and object2 refer to the same object. Object variables refer to objects, they don't contain the object's value. <code>object1.equals(object2)</code> checks for object equivalence.</p> + +<p>The types <code>int</code> and <code>boolean</code> are primitive types. They are not objects. Primitive type variables contain primitive values, so they don't refer to anything. So in this case, <code>==</code> checks for equal values.</p> + +<% + end + } + file = { + title = [[Run File and Comment]] + content = function() +%> + +<p>Now I will return to the Bash command line.</p> + +<code block> +~/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 $ +</code> + +<p><code>bsh.sh</code> runs a BeanShell file from the command line. I will keep modifying <code>code.bsh</code> to show my examples.</p> + +<p> In Java, comments are between <code>/*</code> and <code>*/</code> or between <code>//</code> and the end of the line. Comments are ignored.</p> +<% + end + } + ifs = { + title = [[If Statements]] + content = function() +%> + +<code block> +~/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 $ +</code> + +<p><code>if</code> statements take a boolean argument.</p> + +<% + end + } + loops = { + title = [[Loops]] + content = function() +%> + +<code block> +~/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 $ +</code> + +<p>The first form of the <code>for</code> 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 <code>for</code> loop iterates through anything that is iterable, returning values in sequence. For the fourth loop, see <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html#iterator--">List.iterator()</a>.</p> + +<% + end + } + blocks = { + title = [[Blocks]] + content = function() +%> + +<code block> +~/beanshell $ cat code.bsh +{ + String s = "ok"; + print( "first " + s ); +} +print( "second " + s ); + +~/beanshell $ ./bsh.sh code.bsh +first ok +second void +~/beanshell $ +</code> + +<p>Code between the braces <code>{</code> and <code>}</code> is a block. When braces are part of a statement like a <code>for</code> statement, the block extends to include the whole statement. Variables declared inside a block only exist within that block.</p> + +<p>In BeanShell an undefined variable is <code>void</code> but in Java you would get a compile error.</p> + +<p>Note that besides being an arithmetic operator, <code>+</code> also concatenates strings.</p> +<% + end + } + arrays = { + title = [[Arrays and null]] + content = function() +%> +<code block> +~/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 $ +</code> + +<p>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.</p> + +<p>Arrays are declared with the type that they will hold, and then they can only hold that type. Arrays cannot change size.</p> + +<p>Since arrays don't know how become strings, I used <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-">Arrays.asList()</a> which converts an array into a list so that it can be displayed properly. Note that this is a <code>static</code> function which means that it isn't part of an object, but rather is a general function that is associated with a class.</p> + +<p>In the second part of the code, I assign <code>array</code> to a new array of length 3 with no values. Java uses the special value <code>null</code> to mean no value.</p> +<% + end + } + fns = { + title = [[Functions]] + content = function() +%> +<code block> +~/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 $ +</code> + +<p>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 <code>void</code> means nothing is returned.</p> +<% + end + } + impl = { + title = [[Implementing Interfaces]] + content = function() +%> +<code block> +~/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 $ +</code> + +<p>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 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html">Iterator</a> 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 <code>i</code> keeps track of the current count.</p> + +<p>This example is just to give a taste of object-oriented programming. When you learn Java, you will learn much more about this.</p> +<% + end + } + conclusion = { + title = [[Conclusion]] + content = function() +%> +<p>The purpose of this tutorial is to introduce basic Java concepts that will make learning Java easier. I avoided details and involved examples.</p> + +<p>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.</p> +<% + end + } }