Skip to Content
Learning Java
book

Learning Java

by Jonathan Knudsen, Patrick Niemeyer
May 2000
Beginner
726 pages
21h 42m
English
O'Reilly Media, Inc.
Content preview from Learning Java

Java Statements and Expressions

At the prompt, you can type standard Java statements and expressions. Statements and expressions are all of the normal things that you’d say inside a Java method: e.g., variable declarations and assignments, method calls, loops, and conditionals.

You can type these exactly as they would appear in Java. You also have the option of working with “loosely typed” variables and arguments. That is, you can simply be lazy and not declare the types of variables that you use (both primitives and objects). BeanShell will still give you an error if you attempt to misuse the actual contents of the variable. If you do declare types of variables or primitives, BeanShell will enforce them.

Here are some examples:

foo = "Foo";   
four = (2 + 2)*2/2;
print( foo + " = " + four );   // print( ) is a bsh command

// do a loop
for (i=0; i<5; i++)
    print(i);  

// pop up an AWT frame with a button in it
b = new JButton("My Button");
f = new JFrame("My Frame");
f.getContentPane( ).add(b, "Center");
f.pack( );
f.setVisible(true);
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Java, 4th Edition

Learning Java, 4th Edition

Patrick Niemeyer, Daniel Leuck
Learning Java, 6th Edition

Learning Java, 6th Edition

Marc Loy, Patrick Niemeyer, Daniel Leuck
Learning Java, 5th Edition

Learning Java, 5th Edition

Marc Loy, Patrick Niemeyer, Daniel Leuck
Head First Java, 2nd Edition

Head First Java, 2nd Edition

Kathy Sierra, Bert Bates

Publisher Resources

ISBN: 1565927184Supplemental ContentCatalog PageErrata