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
* 需要處理異常的成員初始化
不能調用會拋出異常的方法
不能對基本異常做任何處理
如果你需要處理錯誤
將初始化的代碼放到構造器或者靜態初始化塊的 try/catch塊中
當然
你也可以放到非靜態的代碼塊中
但是構造器似乎更為通用
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
for
not found
* 類String
類String是不可變的
即使他的某些方法看起來會改變字符串的內容
但實際上他們返回的是一個新的字符串
而不是改變原來的字符串
類String的方法
cancat(String)
trim()
replace(char
char)
類String的靜態方法valueOf能處理所有的基本類型和對象(調用對象的toString()方法)
在substring(int
int)方法中
第二個參數是
不包括
的(譯者注
第一個參數是
包括
的
例如substring(
)將會返回字符串從第二個字符開始(包括第二個字符)
到第五個字符結束(不包括第五個字符)的子字符串)
如果沒有找到
indexOf方法將返回
* 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
==
* 字符串池
虛擬機有一個字符串池
保存著幾乎所有的字符串對象
字符串表達式總是指向字符串池中的一個對象
使用new操作創建的字符串對象不指向字符串池中的對象但是可以使用intern方法使其指向字符串池中的對象(譯者注
如果池中已經有相同的字符串
使用equals方法確定
則直接返回池中的字符串
否則先將字符串添加到池中
再返回)
池中兩個相等的字符串如果使用
==
來比較將返回真
* StringBuffer doesn
t override equals
* 類StringBuffer沒有覆蓋equals方法
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[
];
ii = new int[] {
};
int[] ii = {
); // only when you declare the reference
* 你可以通過new操作或者顯式的初始化創建一個數組對象
ii = new int[
];
ii = new int[] {
};
int[] ii = {
}; // 只有聲明的時候
* CAREFUL: You can
t create an array object with:
int iA[
];
* 小心
你不能象下面這樣創建一個數組對象
int iA[
];
* 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
* 如果你不提供初始值
對象數組的元素總是初始化成null
基本類型數組的元素總是初始化成零
Primitive Types 基本類型 * Primitive types:
short and char are both
bytes
int and float are both
bytes
long and double are both
bytes
char is the only unsigned primitive type
* 基本類型
short和char的長度是兩個字節
int和float的長度都是四個字節
long和double的長度都是八個字節
char是唯一的無符號基本類型
* Literals:
You can have boolean
char
int
long
float
double and String literals
You cannot have byte or short literals
char literals:
d
\u
c
(the
c
must be a
digit hex number)
int literals:
x
c
is hex
is octal(for
)
You can initialize byte
short and char variables with int literals(or const int expressions) provided the int is in the appropriate range
* 表達式
只有boolean
char
int
long
float
double和字符串的表達式
沒有byte和short的表達式
字符(char)表達式
d
\u
c
(
c
必須是四位的十六進制數字)
整型(int)表達式
x
c
是十六進制形式
是八進制形式
可是使用合法范圍內的整型表達式對byte
short和char變量初始化
* CAREFUL: can
t assign a double literal to a float
float fff =
;
* 小心
不能將一個double表達式賦給一個float變量
float fff =
;
* 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
* 基本類型的包裝類
不可變的
覆蓋equals方法
靜態方法valueOf(String)返回的是包裝類而不是基本類型
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
* boolean不能跟其它的任何類型相互轉換
但是boolean
>String是允許的
* 所有的基本類型之間可以通過顯式的類型轉換而轉變成其它類型
* char/byte/short/int/long到float/double的轉換是寬轉換
即使有可能丟掉部分信息
* 窄轉換需要顯式的轉換
整型的窄轉換只簡單的去掉高位比特
所有到char的轉換都是窄轉換(包括byte)因為轉換是從有符號數到無符號數
的轉換
負數將會得到一個混亂的結果
* Widening primitive and reference conversions are allowed for assignment and in matching the arguments to a method (or ctor) call
* 對象和基本類型的寬轉換允許在賦值和匹配的方法調用中(非顯式的)使用
* For assignment (but not method invocation)
representable constant int expressions can be converted to byte
char or shorts (eg
char c =
)
* 賦值時
合法的整型表達式能被自動轉換成byte
char或者short(例如
char c =
)
* Unary numeric promotions: byte/short/char to int
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19140.html