comparison src/beanshell.html.luan @ 88:f320d444face

finish beanshell
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 22 Apr 2025 23:11:59 -0600
parents 5f4cc9d3d3cb
children 416bdc22a478
comparison
equal deleted inserted replaced
87:5f4cc9d3d3cb 88:f320d444face
23 %> 23 %>
24 <p><a href="https://beanshell.github.io/download.html">Download bsh-xx.jar</a> and put it in your working directory.</p> 24 <p><a href="https://beanshell.github.io/download.html">Download bsh-xx.jar</a> and put it in your working directory.</p>
25 25
26 <code block> 26 <code block>
27 ~/beanshell $ ls -F 27 ~/beanshell $ ls -F
28 bsh-2.0b4.jar bsh.sh* 28 bsh-2.0b4.jar bsh_console.sh*
29 ~/beanshell $ cat bsh.sh 29 ~/beanshell $ cat bsh_console.sh
30 #!/bin/bash 30 #!/bin/bash
31 31
32 export CLASSPATH=bsh-2.0b4.jar 32 export CLASSPATH=bsh-2.0b4.jar
33 java bsh.Console & 33 java bsh.Console &
34 ~/beanshell $ 34 ~/beanshell $
35 </code> 35 </code>
36 36
37 <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> 37 <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>
38 38
39 <p>Now you can run BeanShell:</p> 39 <p>Now you can run BeanShell:</p>
40 40
41 <code block> 41 <code block>
42 ~/beanshell $ ./bsh.sh 42 ~/beanshell $ ./bsh_console.sh
43 ~/beanshell $ 43 ~/beanshell $
44 </code> 44 </code>
45 45
46 <p>You should see a new window that lets you interact with BeanShell. From now on I will mostly refer interactions in that window.</p> 46 <p>You should see a new window that lets you interact with BeanShell. From now on I will mostly refer interactions in that window.</p>
47 47
111 bsh % print( message.getClass() ); 111 bsh % print( message.getClass() );
112 class java.lang.String 112 class java.lang.String
113 bsh % 113 bsh %
114 </code> 114 </code>
115 115
116 <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> 116 <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>
117 117
118 <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> 118 <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>
119 119
120 <p>Back to programming. The class of <code>message</code> (a String) is <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html">java.lang.String</a>. Click that link and see all the things that strings can do. For example, they can <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toUpperCase--">toUpperCase</a> and <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--">length</a>. These functions return values. Strings can also <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--">getClass</a> but this functionality comes from the superclass <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html">java.lang.Object</a>. Objects are the base class of everything. Look again at the top of <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html">java.lang.String</a> and you will see the class hierarchy and see that String is a subclass of Object.</p> 120 <p>Back to programming. The class of <code>message</code> (a String) is <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html">java.lang.String</a>. Click that link and see all the things that strings can do. For example, they can <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toUpperCase--">toUpperCase</a> and <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--">length</a>. These functions return values. Strings can also <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--">getClass</a> but this functionality comes from the superclass <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html">java.lang.Object</a>. Objects are the base class of everything. Look again at the top of <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html">java.lang.String</a> and you will see the class hierarchy and see that String is a subclass of Object.</p>
121 121
151 bsh % print( list.getClass() ); 151 bsh % print( list.getClass() );
152 class java.util.ArrayList 152 class java.util.ArrayList
153 bsh % 153 bsh %
154 </code> 154 </code>
155 155
156 <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> 156 <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>
157 157
158 <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> 158 <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>
159 159
160 <p>Java organizes classes into packages. The package of List and ArrayList is <a href="https://docs.oracle.com/javase/8/docs/api/java/util/package-summary.html">java.util</a>. 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.</p> 160 <p>Java organizes classes into packages. The package of List and ArrayList is <a href="https://docs.oracle.com/javase/8/docs/api/java/util/package-summary.html">java.util</a>. 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.</p>
161 161
162 162
163 <%
164 end
165 }
166 boolean = {
167 title = [[Boolean Values and Equality]]
168 content = function()
169 %>
170
171 <code block>
172 bsh % print( 3 > 4 );
173 false
174 bsh % print( 3 >= 4 );
175 false
176 bsh % print( 3 < 4 );
177 true
178 bsh % print( 3 <= 4 );
179 true
180 bsh % print( 3 == 4 );
181 false
182 bsh % print( 3 != 4 );
183 true
184 bsh % print( 3 < 4 && 3 > 4 );
185 false
186 bsh % print( 3 < 4 || 3 > 4 );
187 true
188 bsh % boolean b = 3 < 4;
189 bsh % print(b);
190 true
191 bsh % print( b == true );
192 true
193 bsh %
194 </code>
195
196 <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>
197
198 <code block>
199 bsh % String s1 = new String("hi");
200 bsh % String s2 = new String("hi");
201 bsh % print( s1 == s2 );
202 false
203 bsh % print( s1.equals(s2) );
204 true
205 bsh % String s3 = s1;
206 bsh % print( s1 == s3 );
207 true
208 bsh %
209 </code>
210
211 <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>
212
213 <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>
214
215 <%
216 end
217 }
218 file = {
219 title = [[Run File and Comment]]
220 content = function()
221 %>
222
223 <p>Now I will return to the Bash command line.</p>
224
225 <code block>
226 ~/beanshell $ ls -F
227 bsh-2.0b4.jar bsh.sh* bsh_console.sh* code.bsh
228 ~/beanshell $ cat bsh.sh
229 #!/bin/bash
230
231 export CLASSPATH=bsh-2.0b4.jar
232 java bsh.Interpreter $*
233 ~/beanshell $ cat code.bsh
234 /* This is a comment */
235 // This is also a comment
236
237 print("Hi");
238 print("Bye");
239
240 ~/beanshell $ ./bsh.sh code.bsh
241 Hi
242 Bye
243 ~/beanshell $
244 </code>
245
246 <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>
247
248 <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>
249 <%
250 end
251 }
252 ifs = {
253 title = [[If Statements]]
254 content = function()
255 %>
256
257 <code block>
258 ~/beanshell $ cat code.bsh
259 int x = 5;
260
261 print("first if");
262 if( x == 5 ) {
263 print("x == 5");
264 }
265
266 print("second if");
267 if( x > 5 ) {
268 print("x > 5");
269 } else {
270 print("x <= 5");
271 }
272
273 print("third if");
274 if( x > 5 ) {
275 print("x > 5");
276 } else if( x < 5 ) {
277 print("x < 5");
278 } else {
279 print("x == 5");
280 }
281
282 ~/beanshell $ ./bsh.sh code.bsh
283 first if
284 x == 5
285 second if
286 x <= 5
287 third if
288 x == 5
289 ~/beanshell $
290 </code>
291
292 <p><code>if</code> statements take a boolean argument.</p>
293
294 <%
295 end
296 }
297 loops = {
298 title = [[Loops]]
299 content = function()
300 %>
301
302 <code block>
303 ~/beanshell $ cat code.bsh
304 ~/beanshell $ cat code.bsh
305 print("first loop");
306 int i = 1;
307 while( i <= 3 ) {
308 print(i);
309 i = i + 1;
310 }
311
312 print("second loop");
313 for( int i = 1; i <= 3; i = i + 1 ) {
314 print(i);
315 }
316
317 List list = new ArrayList();
318 list.add("a");
319 list.add("b");
320 list.add("c");
321
322 print("third loop");
323 for( int i = 0; i < list.size(); i = i + 1 ) {
324 String s = list.get(i);
325 print(s);
326 }
327
328 print("fourth loop");
329 Iterator iter = list.iterator();
330 while( iter.hasNext() ) {
331 String s = iter.next();
332 print(s);
333 }
334
335 print("fifth loop");
336 for( String s : list ) {
337 print(s);
338 }
339
340 ~/beanshell $ ./bsh.sh code.bsh
341 first loop
342 1
343 2
344 3
345 second loop
346 1
347 2
348 3
349 third loop
350 a
351 b
352 c
353 fourth loop
354 a
355 b
356 c
357 fifth loop
358 a
359 b
360 c
361 ~/beanshell $
362 </code>
363
364 <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>
365
366 <%
367 end
368 }
369 blocks = {
370 title = [[Blocks]]
371 content = function()
372 %>
373
374 <code block>
375 ~/beanshell $ cat code.bsh
376 {
377 String s = "ok";
378 print( "first " + s );
379 }
380 print( "second " + s );
381
382 ~/beanshell $ ./bsh.sh code.bsh
383 first ok
384 second void
385 ~/beanshell $
386 </code>
387
388 <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>
389
390 <p>In BeanShell an undefined variable is <code>void</code> but in Java you would get a compile error.</p>
391
392 <p>Note that besides being an arithmetic operator, <code>+</code> also concatenates strings.</p>
393 <%
394 end
395 }
396 arrays = {
397 title = [[Arrays and null]]
398 content = function()
399 %>
400 <code block>
401 ~/beanshell $ cat code.bsh
402 String[] array = { "a", "b", "c", "d", "e" };
403 print( "array = " + Arrays.asList(array) );
404 print( "length = " + array.length );
405 print( "array[1] = " + array[1] );
406 array[1] = "bb";
407 print( "modified array = " + Arrays.asList(array) );
408 print("loop");
409 for( String s : array ) {
410 print(s);
411 }
412
413 array = new String[3];
414 print( "new array = " + Arrays.asList(array) );
415 array[2] = "two";
416 print( "modified new array = " + Arrays.asList(array) );
417
418 ~/beanshell $ ./bsh.sh code.bsh
419 array = [a, b, c, d, e]
420 length = 5
421 array[1] = b
422 modified array = [a, bb, c, d, e]
423 loop
424 a
425 bb
426 c
427 d
428 e
429 new array = [null, null, null]
430 modified new array = [null, null, two]
431 ~/beanshell $
432 </code>
433
434 <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>
435
436 <p>Arrays are declared with the type that they will hold, and then they can only hold that type. Arrays cannot change size.</p>
437
438 <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>
439
440 <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>
441 <%
442 end
443 }
444 fns = {
445 title = [[Functions]]
446 content = function()
447 %>
448 <code block>
449 ~/beanshell $ cat code.bsh
450 int add(int x,int y) {
451 return x + y;
452 }
453
454 void addAndShow(int x,int y) {
455 int sum = x + y;
456 print( x + " + " + y + " = " + sum );
457 }
458
459 void sayBye() {
460 print("Bye");
461 }
462
463 print( "add(2,3) = " + add(2,3) );
464 addAndShow(2,3);
465 sayBye();
466
467 ~/beanshell $ ./bsh.sh code.bsh
468 add(2,3) = 5
469 2 + 3 = 5
470 Bye
471 ~/beanshell $
472 </code>
473
474 <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>
475 <%
476 end
477 }
478 impl = {
479 title = [[Implementing Interfaces]]
480 content = function()
481 %>
482 <code block>
483 ~/beanshell $ cat code.bsh
484 Iterator iter = new Iterator() {
485 int i = 0;
486 boolean hasNext() {
487 return i < 10;
488 }
489 int next() {
490 i = i + 1;
491 return i;
492 }
493 };
494
495 while( iter.hasNext() ) {
496 print( iter.next() );
497 }
498
499 ~/beanshell $ ./bsh.sh code.bsh
500 1
501 2
502 3
503 4
504 5
505 6
506 7
507 8
508 9
509 10
510 ~/beanshell $
511 </code>
512
513 <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>
514
515 <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>
516 <%
517 end
518 }
519 conclusion = {
520 title = [[Conclusion]]
521 content = function()
522 %>
523 <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>
524
525 <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>
163 <% 526 <%
164 end 527 end
165 } 528 }
166 } 529 }
167 530