Initialization 初始化 * All class-level (member) variables are initialized before they can be used. All local variables are not initialized until it is done eXPlicitly.
* 所有的主成員在他們使用之前被初始化所有的局部變量必須通過顯式的賦值來初始化
* An array object (as distinct from reference) is always initialized(with zeroes or nulls)
* 數組對象總是能夠初始化(零或者null)
* Member initialization with the declaration has exception PRoblems: - cannot call methods that throw a checked exception. - cannot do error recovery from runtime exceptions. - If you need to deal with errors you can put the initialization code along with try/catch statements in either a ctor (for instance fields) or in a static initialization block for static fields. You can also have instance (non-static) initialization blocks but ctors are more recognizable.
Strings 字符串 * The String class - Because string is an immutable class, its instance methods that look like they would transform the object they are invoked upon, do not alter the object and instead return new String objects. - String has methods concat(String),trim(),replace(char,char) - String has static valueOf methods for a whole bunch of primitives and for Object too (equivalent to Object.toString()). - in substring(int,int), the second arg is exclusive. - indexOf methods returns -1 for 'not found'
* String Pool: A JVM has a string pool where it keeps at most one object of any String. String literals always refer to an object in the string pool. String objects created with the new Operator do not refer to objects in the string pool but can be made to using String's intern() method. Two String references to 'equal' strings in the string pool will be '=='.
Arrays 數組 * Arrays are objects .. the following create a reference for an int array. int[] ii; int ii[];
* 數組是一個對象 .. 下面的代碼創建一個整型數組的引用: int[] ii; int ii[];
* You can create an array object with new or an explicit initializer: ii = new int[3]; ii = new int[] { 1,2,3 }; int[] ii = { 1,2,3 ); // only when you declare the reference.
* 你可以通過new操作或者顯式的初始化創建一個數組對象: ii = new int[3]; ii = new int[] { 1,2,3 }; int[] ii = { 1,2,3 }; // 只有聲明的時候
* CAREFUL: You can't create an array object with: int iA[3];
* 小心:你不能象下面這樣創建一個數組對象: int iA[3]; * If you don't provides values, the elements of obj arrays are always initialized to null and those of primitive arrays are always initialized to 0.
* 假如你不提供初始值,對象數組的元素總是初始化成null,基本類型數組的元素總是初始化成零
Primitive Types 基本類型 * Primitive types: - short and char are both 2 bytes. int and float are both 4 bytes. long and double are both 8 bytes. - char is the only unsigned primitive type.
* Literals: - You can have boolean, char, int, long, float, double and String literals. You cannot have byte or short literals. - char literals: 'd' '/u0c20' (the 0c20 must be a 4-digit hex number). - int literals: 0x3c0 is hex, 010 is octal(for 8). - You can initialize byte, short and char variables with int literals(or const int expressions) provided the int is in the appropriate range.
* The only bit operators allowed for booleans are &^ (cant do ~ or shift ops)
* 位運算只有&^(不能使用~或者移位操作)
* Primitive wrapper classes - are immutable. - override equals. - the static valueOf(String) methods in primitive wrapper classes return wrapper objects rather than a primitives.
Conversions and Promotions 類型轉換 * boolean->anything but boolean or string is not allowed. * All other primitive conversions are allowed with an explicit cast. * char/byte/short/int/long to float/double is a widening conversion even if some precision is lost (the overall magnitude is always preserved). * Narrowing conversions require an explicit cast. - integral narrowing conversions simply discard high-order bits. - anything to char is a narrowing conversion (inc byte) because its signed to unsigned and negative numbers get messed up