1.Java沒有指針?No,Java只是沒有針對指針的算法。引用其實已經(jīng)包含了指針。
2.RTTI——Runtime type identification,作用就是告訴你整個object是屬于什么類型的。最常用的是getClass()方法。
3.只在for循環(huán)中用到的變量,就在for循環(huán)中聲明和分配,不用擔心會循環(huán)分配內(nèi)存。
比如:
for(int i =0;i<10;i++){
String s = a[i];
}
s并不會循環(huán)new,編譯器會處理,讓它只在第一次分配內(nèi)存。
前面編程,我還特意把s移到外面去呢……汗啊
4.switch語句中,如果在default語句中加一個break是完全沒有用的。因為在那個地方的break根本就不會被執(zhí)行。
5.continue
for(int idx=0;idx<1000,idx++){
if(idx==555){continue;}
}
在這個例子中,循環(huán)僅僅跳過了555,如果程序中遇到類似要跳過某個點的情況,可以這么利用continue,會減少很多代碼。
6.chaining constructor
活用super()、this()等,來縮減構(gòu)造函數(shù)的復(fù)雜度。
7.private for attributes
public for public methods
protected for helper methods
8.In Java, instances of the same class are friends and give unrestricted access to all of their properties and methods.
比如
if((obj.value==5)&&(this.value==5)){
obj.value=25;
}
這里面跳過了setter方法,直接給屬性賦值,這是很危險的做法。解決的方法是無論什么方法給對象中的屬性賦值都要通過setter方法。
9.對于問題代碼要盡量用throw exception告知用戶,因為有時候不知道代碼的執(zhí)行環(huán)境,如EJB等等,很多環(huán)境都是沒有Console的,若是簡單的System.out.println 可能解決不了問題。