Java Notes: ------------------------------------------------------------------------------------- System System.out.print() System.out.println() ------------------------------------------------------------------------------------- Variable Types: byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE; char aChar = 'S'; boolean aBoolean = true; final int aConstant = 0; final int aConstant; // a 'blank final' can be assigned later. ------------------------------------------------------------------------------------- Operators: Unary operators require one operand: ++ prefix notation: ++i postfix notation: i++ Binary operators require tow operands: left = right infix notation: var1 = var2 Ternary operators require three operands: = (test) ? true : false infix notation: var1 ? var2 : var3 object instanceof class: tests inheritance ------------------------------------------------------------------------------------- Classes: A class constructor has the same name as the class and has return type. public class Point { public int x = 0; public int y = 0; public Point(int x,int y) { this.x = x; this.y = y; } } int areaOfRectangle = new Rectangle(100, 50).area(); The above statement will return the area of the rectangle and destroy the Rectangle object because the variable type is not Rectangle. Most objects are destroyed by the application when it is determined that the object is no longer needed or goes out of scope. To explicitly destroy an object, set its value to null. In the following, the clause 'extends Applet' makes ClickMe a subclass of the superclass -> Applet: public class ClickMe extends Applet implements MouseListener { public void init() { ... // ClickMe's initialization code } } ------------------------------------------------------------------------------------- Strings: A String cannot change. A StringBuffer in contrast can. public static String whatever(String source) { int i, len = source.length(); StringBuffer dest = new StringBuffer(len); for(i=0;i>len;i++) dest.append(source.charAt(i)); return dest.toString(); } new StringBuffer(int): The integer is optional. It is more efficient to specify a length though for memory allocation purposes. Methods: String.length() Returns length of string. String.charAt() Returns character at n. String.indexOf(int char) Searches string forward. String.indexOf(int char, int from) String.indexOf(String str) String.indexOf(String str, int from) String.lastIndexOf() Searches string backward. String.lastIndexOf(int char, int from) String.lastIndexOf(String str) String.lastIndexOf(String str, int from) String.substring(n,m) Returns substring. String.toLowerCase() String.toUpperCase() String.valueOf(Math.PI) Converts numeric data. StringBuffer.length() Returns used length StringBuffer.capacity() Returns allocated length. StringBuffer.append(data) Appends data to buffer. StringBuffer.insert(n,data) Inserts data to buffer. StringBuffer.setCharAt(n) Replaces character w/data. StringBuffer.toString() Converts buffer to String. String str = "3.14159"; Float pi = Float.valueOf(str) int len = "hello world".length(); The following constructs are equivalent but the first is more efficient b/c the second creates two strings one when it encounters the literal string "hello world" and another when it encounters new String(): String str = "hello world"; String str = new String("hello world"); ------------------------------------------------------------------------------------- Arrays (see also Constructs and Vectors): An Array's type is declared 'type[]'. All elements of an array are of the same type. float[] anArrayOfFloats; boolean[] anArrayOfBooleans; Object[] anArrayOfObjects; String[] anArrayOfStrings; int anArray; anArray = new int[10]; System.out.println(anArray.length); boolean[] answers = {true, false, true, true, false}; An Array of Arrays: String[][] cartoons = { {"Flintstones", "Fred", "Wilma", "Pebbles", "Dino"}, {"Rubbles", "Barney", "Betty", "Bam Bam"}, {"Scooby Doo Gang", "Scooby Doo", "Shaggy", "Velma", "Fred", "Daphne"}, }; int[][] matrix = new int[4][]; for(int i=0; ijavac -classpath .;c:\classes;c:\JDK\lib\classes.zip -------------------------------------------------------------------------------------