Home | Back | Contents | Next |
/* Standard Java syntax */ // Use a hashtable Hashtable hashtable = new Hashtable(); Date date = new Date(); hashtable.put( "today", date ); // Print the current clock value print( System.currentTimeMillis() ); // Loop for (int i=0; i<5; i++) print(i); // Pop up a frame with a button in it JButton button = new JButton( "My Button" ); JFrame frame = new JFrame( "My Frame" ); frame.getContentPane().add( button, "Center" ); frame.pack(); frame.setVisible(true); |
/* Loosely Typed Java syntax */ // Use a hashtable hashtable = new Hashtable(); date = new Date(); hashtable.put( "today", date ); // Print the current clock value print( System.currentTimeMillis() ); // Loop for (i=0; i<5; i++) print(i); // Pop up a frame with a button in it button = new JButton( "My Button" ); frame = new JFrame( "My Frame" ); frame.getContentPane().add( button, "Center" ); frame.pack(); frame.setVisible(true); |
try { int i = 1/0; } catch ( ArithmeticException e ) { print( e ); } |
try { ... } catch ( e ) { print( "caught exception: "+e ); } |
Note: As of BeanShell version 1.3 the default scoping of loosely typed variables was changed to be more consistent with Java. BeanShell still supports an alternate scoping used in earlier versions. This mode can be enabled for legacy code by setting the system property "localscoping" to true. See appendix "Local Scoping". |
// Arbitrary code block { y = 2; // Untyped variable assigned int x = 1; // Typed variable assigned } print( y ); // 2 print( x ); // Error! x is undefined. // Same with any block statement: if, while, try/catch, etc. if ( true ) { y = 2; // Untyped variable assigned int x = 1; // Typed variable assigned } print( y ); // 2 print( x ); // Error! x is undefined. |
for( int i=0; i<10; i++ ) { // typed for-init variable j=42; } print( i ); // Error! 'i' is undefined. print( j ); // 42 for( z=0; z<10; z++ ) { } // untyped for-init variable print( z ); // 10 |
button = new java.awt.Button(); button.label = "my button"; // Equivalent to: b.setLabel("my button"); print( button.label ); // Equivalent to print( b.getLabel() ); |
Float f = new Float(42f); print( f.infinite ); // Equivalent to print( f.isInfinite() ); // false |
b = new java.awt.Button(); b{"label"} = "my button"; // Equivalent to: b.setLabel("my button"); h = new Hashtable(); h{"foo"} = "bar"; // Equivalent to: h.put("foo", "bar"); |
List foo = getSomeList(); for ( untypedElement : foo ) print( untypedElement ); for ( Object typedElement: foo ) print( typedElement ); int [] array = new int [] { 1, 2, 3 }; for( i : array ) print(i); for( char c : "a string" ) print( c ); |
dateobj = new Date(); switch( dateobj ) { case newYears: break; case christmas: break; default: } |
int i=5; Integer iw = new Integer(5); print( i * iw ); // 25 Vector v = new Vector(); v.put(1); int x = v.getFirstElement(); |
// Standard Java import javax.xml.parsers.*; import mypackage.MyClass; |
import *; |
Tip: The BeanShell which() command will use the classpath mapping capability to tell you where exactly in your classpath a specified class is located: bsh % which( java.lang.String ); Jar: file:/usr/java/j2sdk1.4.0/jre/lib/rt.jar |
importCommands("/bsh/commands"); |
Tip: The classes java.awt.List and java.util.List are both imported by default. Because java.util.List is imported later, as part of the java.util package, it takes precedence. To access java.awt.List simply import it in, or the java.awt package again your script. Later imports take precedence. |
@gt | > |
@lt | < |
@lteq | <= |
@gteq | >= |
@or | || |
@and | && |
@bitwise_and | & |
@bitwise_or | | |
@left_shift | << |
@right_shift | >> |
@right_unsigned_shift | >>> |
@and_assign | &= |
@or_assign | |= |
@left_shift_assign | <<= |
@right_shift_assign | >>= |
@right_unsigned_shift_assign | >>>= |
Home | Back | Contents | Next |