http://java.sun.com/docs/books/tutorial/java/index.html
http://java.sun.com/products/jdk/1.1/docs/api/packages.html
package games.poker; // always the first declaration in a file
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.*;
class Circle
{
static final double PI = 3.14159;
double x, y; // the center of the circle
double radius;
Circle(double nx, double ny, double nr) {x=nx; y=ny; r=nr;}
Circle(double nr) {this(0.0; 0.0, nr);}
double area() { return PI * radius * radius; }
double circumference() { return 2 * PI * radius; }
static double nTimesPI(int n) {return n * PI;}
}
class Circle
{
static final double PI = 3.14159;
double x, y;
double radius;
...
}
class Circle
{
class Circle
{
Circle c2 = new Circle(1.0, 2.0, 3.0); // calls first constructor Circle c1 = new Circle(1.9); // calls second constructor
double aSum = c1.area() + c2.area(); double rSum = c1.radius + c2.radius;
double fourPI = c1.nTimesPI(2) + Circle.nTimesPI(2);
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
if ( v1.equals(v2) ) // they are equal!
char s[] = new char[100]; char[] s1 = new char[50]; // brackets may go either place in declaration
for (int i=0; i<s.length; i++)
int i = -1, j = 100; char c = s[i] + s[j] ; // either will throw IndexOutOfBoundsException
Circle[] ca = new Circle[100]; // elements are all null
Circle[] ca1 = {new Circle(1.0), new Circle(2.0), new Circle(3.0)};
char[] s2 = {'a', 'b', 'c'};
Button buttons[] = new Button[10]; // array of 10 null pointers
for (int i=0; i<buttons.length; i++)
buttons[i] = new Button("label " + i); // must allocate elements
String s = "Hello there";
char[] a = {'H','e','l','l','o',' ','t','h','e','r','e'};
String s = new String(a);
String s2 = s + " everyone";
s2 += " here";
s2 = s2.concat(" and now");
StringBuffer b = new StringBuffer("Hello");
b.append(" there");
b.setCharAt(0, 'M');
b.setLength(b.length()-3); // chops off last three characters
String s = b.toString(); // converts b to a String
StringBuffer b1(100); // initial value is "" b1.capacity() // returns 100 StringBuffers are reallocated if more space is necessary
class Outer
{
int x;
class Inner
{
int y;
// can reference x
}
}
class Outer
{
int x;
static class ListNode
{
int info;
ListNode next;
ListNode(int newInfo, ListNode newNext)
{
info = newInfo;
next = newNext;
// can't reference x
}
}
}
class ListNode
{
int info;
ListNode next;
static int length(ListNode l)
{
return l == null ? 0 : 1 + length(l.next);
}
}
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]);
outer:for (...)
for (...)
break outer;