import java.applet.*;
import java.awt.*;
public class HiApplet
extends Applet
{
private String[] messages =
{"Hello", "Hi", "How Are You?", "What's New?"};
private int index = 0;
public void init()
{
add(new MsgButton(this));
}
public void paint(Graphics g)
{
g.drawRect(0,0,size().width-1,size().height-1);
}
public boolean mouseDown(Event e, int x, int y)
{
getGraphics().drawString(messages[index], x, y);
return true;
}
void changeMessage()
{
index = (index + 1) % messages.length;
}
}
class MsgButton
extends Button
{
HiApplet parent;
public MsgButton(HiApplet parent)
{
super("Change Message");
this.parent = parent;
}
public boolean action(Event e, Object label)
{
parent.changeMessage();
return true;
}
}
public static void main(String argv[]) {...}
public class Echo
{
public static void main(String argv[])
{
for (int i=0; i<argv.length; i++)
System.out.print(argv[i] + " ");
System.out.print("\n");
}
}
System.exit(0);
String homedir = System.getProperty("user.home");
String debug = System.getProperty("myapp.debug");
% java -Dmyapp.debug=true myapp
package games.poker; // always the first declaration in a file
setenv CLASSPATH .:~/classes:/usr/local/classes
edu.uci.ics.catalina.my_pack.my_class.display();
import java.util; // can access util.Hashtable
import java.util.Hashtable; // can access Hashtable
import java.util.*; // can access all classes within util
import java.lang.*;
/* This is a (potentially) multi-line comment */ // This is a single line comment /** This is a special documentation comment */
public final class Math {
public static final double PI = 3.14159;
}
\uxxxx // where xxxx is four hexidecimal digits for the code
\n \r \t etc.
Button a = new button("On");
Button b = new button("Off");
a = b; // now both a and b refer to object labelled Off
int x = 1;
int y = 2;
x = y; // now x and y both have the value 2, but they have distinct storage
Vector v1 = new Vector; Vector v2 = v1.clone(); // v2 is a deep copy of v1
if (v1.equals(v2)) // they are equal!
String s = "This is a string";
ComplexNumber c = new ConmplexNumber(1.0,0.0); double magnitude = c.magnitude();
Button buttons[] = new Button[10];
for (int i=0; i<buttons.length; i++)
buttons[i] = new Button("label " + i); // must allocate elements
int table[] = {1, 2, 4, 8, 16, 32, 128, 256}; // also allocates an array
table[8] // throws IndexOutOfBoundsException
object instanceof type
s1 + s2 + i // converts non-String to String first, via toString method
i >>> 1
while (p != null) ... if (i != 0) ...
int i, j;
for (i=0, j=1; i*j < 100; i++, j--) ...
int a[] = {0,1,2,3,4,5,6,7,8,9};
for (int k=0; k<a.length; k++)
System.out.println("a[" + k + "] = " + a[k]);
// like C++, scope of k is within loop
outer:for (...)
for (...)
break outer;
try // this statement execute, if an exception is thrown, its caught below catch (SomeException e) // come here if SomeException is thrown catch (OtherException e) // come here if OtherException is thrown finally // always excute this statement no matter how we leave the try block
public void open_file() throws SomeException, OtherException {
throw new SomeException("Oops!");
}