javac XXX.java. java XXX . main method. public static void main(String[] args) java ClassName . String[].
The return tpye is void. It is no different from other methods. args is a String array. You can use
args.length to access the length of args.args[0], args[1]... to access the elements in
the array args[i] (i.e.
i >= args.length ) an error will occur
during runtime. java ClassName string0 string1 .. String[] args = {string0, string1 , ... } is created args pass to main(String[] args) and the method body is
executedargs is not null but args.length is 0. java ClassName "Charles Li". System.out.println(string) to print out a string
and then go to next line.System.out.print(String) to print out a string without
going to a new line at the end. System.out.println(....) can print out
int, float,... as well. public, class, void
....myName is different from
MyName. byte, short, int, long, float, double, char, boolean. long is more precise than int,
double is more precise than float etc etc. int a, b, c
double are
double a = 3.14, b = 3, c = 2.713e10, d = 34.665e5d;
float are very similar to
double. You have to add f at the end of the number.
float a = 3.14f, b= 34.665e5f;
float a = 3.14 causes an compiling error because
3.14 is a double by defalt long x = 58819394L; . int char represents a character. boolean a = true; boolean b = false;
if, else, while, do-while, for, switch .
ClassName[] arrayName arrayName = new
ClassName[size]. null.
int[] array1 = {1,2,3,4,5};
String[] array2 = {"this", "is" , "review" , "notes"}
array.length to get the size of the array.
String by
String s = "Hi"; + String
class. But you should understand the methods. Documentation will be
provided. new. private, protected and public and their meaning. static variables and
non-static variables. this and super. final. importimport java.awt.*. import javax.swing.JFrame.javax.swing.JFrame f = new javax.swing.JFrame("title").
JFrame and override paint(Graphics g) methodFont class and Color class, how to set/get font and colordrawLine, drawRect, fillRect, drawOval, fillOval, drawString methodsgetContentPane()setSize, setVisible, setLocation....BorderLayout, FlowLayout, GridLayoutnew GridLayout(0, 5), new GridLayout(5,0) mean?add(component), remove(index), add(component, index)JPanel(), JPanel(layout)getText, setTextsetBackground
public class XXX implements ActionListener {
.............
............
public void actionPerformed(ActionEvent e) {
// your codes
}
}
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// your codes
}
});
ComponentJButton button = (JButton)e.getSource();
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1) {
// do something
} else if( e.getSource() == button2) {
// do something
} ......
.....
}
String) of the sourcebutton.setActionCommand(String s) set the action command of the button to s.
If you don't set it, the default value is the text on the button.
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if(action.equals(string1)) {
// do something
} else if (action.equals(string2)) {
// do something
} .....
.....
RuntimeException, Checked exceptionRuntimeExceptions are classes that extends RuntimeException.
someMethod(......) throws SomeException {
// your code
}
or
try {
// some codes
} catch (ExceptionX ex) {
// some codes
} catch (ExceptionY ex) {
// some codes
}
.....
RuntimeException. NullPointerException, ArraryIndexOutOfBoundsException, ArithmeticException, NumberFormatException are
all RuntimeExceptions. Understand when will these exceptions occur. FileNotFoundException, IOException are checked exceptions. You should know when these exceptions occur.
try {
line1
line2
line3
......
line10
} catch (ExceptionX ex) {
lineX
......
} catch (ExceptionY ex) {
lineY
.......
} finally {
......
}
line101
line102
If the first exception occurs at line3, then the program will find out which exception matches the exceptin thrown by line 3. The
codes in the catch block will be executed. After that, the codes in finally block will be executed. And then line 101.......
public class ExceptionTest {
public static void main(String[] args) {
try {
System.out.println("line 1");
String[] s = new String[10];
System.out.println("line 2");
s[0] = "Hi";
System.out.println("line 3");
System.out.println(s[11]);
System.out.println("line 4");
System.out.println(s[1].toUpperCase());
System.out.println("line 5");
System.out.println(10/0);
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("line X");
} catch (NullPointerException ex) {
System.out.println("line Y");
} catch (ArithmeticException ex) {
System.out.println("line Z");
} finally {
System.out.println("line finally");
}
System.out.println("Start all over");
}
}
public static void main(String[] args) throws Exception {
// your codes
}
or
public static void main(String[] args) {
try {
// your codes
} catch (Exception ex) {
// catch all exception because all exception class are subclass of Exception
}
}
InputStream
FileInputStream in = new FileInputStream(filename)int read()
int ch = -1;
while((ch = in.read())!= -1) {
// do something
}
BufferedReader:
BufferedReader in = new BufferedReader(new FileReader(filename));: create a BufferedReader from a file. new BufferedReader(new InputStreamReader(System.in));: create a BufferedReader from user inputString readLine:
String line = null;
while((line = in.readLine())!=null) {
// do something
}
FileOutputStream
FileOutputStream out = new FileOutputStream(filename);out.write(int ch)PrintStream
PrintStream out = new PrintStream(new FileOutputStream(filename));out.print(...), out.println(....)System.out is a PrintStreamIOException or FileNotFoundExceptionclose()read, write methods.String[] split(String exp) splits a String by exp. Example
String s = "123ab456abXXX", then s.split("ab") returns {"123", "456", "XXX"}
equals (vs ==) and how to write equals method.toString (System.out.println(obj))instanceof: obj instanceof aClassName returns true if obj is of type aClassName.
Returns false otherwise.Double x = new Double(1.23); double = x.doubleValue();
Double.parseDouble() or Double.MAX_VALUECharacter class. Example, Character.isUpperCase(char ch),
Character.isLetter(char ch)Math class. Example Math.sqrt(5) is used to calculate the square root of 5. Math.random() generates a random double between 0 and 0.99999999. To generate an integer random number
from 0 to max-1, use (int)(max*Math.random()).
add(Object obj), get(int index), set(int index, Object obj, size(), remove(int index), remove(Object obj)boolean contains(Object obj),int indexOf(Object obj) : these methods use equals method
to test equality(String)vector.get(5) get method returns Object.vector.add(new Integer(101)); you can' t add primitive type to a vector. instead, you have to use type wrapper class. ((Integer)vector.get(2)).intValue() returns int