Chapter 1 A Quick Tour

1)      boolean  /?buli?n/

2)      char: 16-bit Unicode UTF-16 character(unsigned)

3)      long: 64-bit integer(signed)

4)     A final field or variable is one that once initialized can never have its value changed.

Named Constants: static final int MAX = 50;

The order of the modifiers final and static makes no difference.

5)     Java program is written in Unicode Characters, that means the statement:

static final int 年龄=27;

is legal.

6)     DO NOT CONFUSE object with object reference.

Object o; //declare a reference without initialization

Object o = new Object(); //declare and initialize an object of Object

Code:

public class Point {
	private int x, y;
	Point(int x, int y) {
		this.x = x;
		this.y = y;
	}
	void set(int x, int y) {
		this.x = x;
		this.y = y;
	}
	void print() {
		System.out.println(x + "," + y);
	}
	public static void main(String[] args) {
		Point p1 = new Point(1, 1);
		Point p2 = new Point(2, 2);
		p1.print();
		p2.print();
		p2 = p1; // now object p1 and p2 refer to the same object
		p1.print();
		p2.print();
		p1.set(3, 3);
		p1.print();
		p2.print();
	}
}

Output:

1,1

2,2

1,1

1,1

3,3

3,3

7)     An array’s length is fixed when it is created and can never change, and array objects have a length field that tells how many elements the array contains.

8)     Comparing String objects:

a)     if( str1.equals(str2) ) // test whether str1 and str2’s contents are same

b)     if( str1==str2 ) //test whether str1 and str2 refer to the same String object

Code 1:

public class StringTest {
	public static void main(String[] args) {
		String str1 = "JavaBeta";
		String str = "Java";
		String str2 = str + "Beta";
		if (str1.equals(str2))
			System.out.println("Content Identical");
		else
			System.out.println("Content NOT Identical");
		if (str1 == str2)
			System.out.println("Reference Identical");
		else
			System.out.println("Reference NOT Identical");
	}
}

Output 1:
Content Identical

Reference NOT Identical

Code 2:

public class StringTest {
	public static void main(String[] args) {
		String str1 = "JavaBeta";
		String str2 = "JavaBeta";
		if (str1.equals(str2))
			System.out.println("Content Identical");
		else
			System.out.println("Content NOT Identical");
		if (str1 == str2)
			System.out.println("Reference Identical");
		else
			System.out.println("Reference NOT Identical");
		str1 += "Beta";
		if (str1.equals(str2))
			System.out.println("Content Identical");
		else
			System.out.println("Content NOT Identical");
		if (str1 == str2)
			System.out.println("Reference Identical");
		else
			System.out.println("Reference NOT Identical");
		System.out.println(str1);
		System.out.println(str2);
	}
}

Output 2:
Content Identical
Reference Identical
Content NOT Identical
Reference NOT Identical
JavaBetaBeta
JavaBeta

Explanation:
Initially, str1 and str2 refers to the same object “JavaBeta”, while str1 is changed, so str1 and str2 has their own copy.

9)    Get used to using %n instead of \n to insert a line-separator.
10)  Classes that do not explicitly extend any other class implicitly extend the Object class.
11)  In Java, you can define multiple top level classes in a single file, providing that at most one of these is public.
12)  Wild Card:
Code:

public interface Lookup {
	T find(String str);
}
public class IntegerLookup implements Lookup {
	public Integer find(String str) {
		if (str == null || str.length() == 0)
			return null;
		return new Integer(1);
	}
}
public class Run {
	public static void processValues(String str, Lookup table) {
		Object o = table.find(str);
		if (o != null)
			System.out.println(o.toString());
		else
			System.out.println("null String");
	}
	public static void main(String[] args) {
		String str1 = null;
		String str2 = "JavaBeta";
		IntegerLookup tmp = new IntegerLookup();
		Run.processValues(str1, tmp);
		Run.processValues(str2, tmp);
	}
}

Output:
null String
1

The wild card is written as ? and is read as “of unspecified type”, or just as “of some type”. Bounded wild card could be written as? extends Number, that is the type of the Generic Type has to be subtype of Number.

13)  Checked Exceptions and Unchecked Exceptions:
Code:

class BadDataSetException extends Exception{ }
 
class MyUtilities {
    public double[] getDataSet(String setName) throws BadDataSetException {
        String file = setName +".dset";
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            return readDataSet(in);
        } catch (IOException e) {
            throw new BadDataSetException();
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                ;    // ignore: we either read the data OK
                     // or we're throwingBadDataSetException
            }
        }
    }
    // ... definition of readDataSet ...
}

If a method's execution can result in checked exceptions being thrown, it must declare the types of these exceptions in a throws clause, as shown for the getdataSet method. A method can throw only those checked exceptions it declares. This is why they are called checked exceptions.  It may throw those exceptions directly with throw or indirectly by invoking a method that throws exceptions. Exceptions of type RuntimeException, Error, or subclasses of these exception types are unchecked exceptions and can be thrown anywhere, without being declared.

14)  Annotations provide information about your program, or the elements of that program, in a structured way that is amenable to automated processing by external tools.
Example:

@interface Author {
	String author();
	int date();
}
@Author(author = "JavaBeta", date = 20140218)
public class Annotation {
	public static void main(String[] args) {
		System.out.println("Annotation");
	}
}