java學(xué)習技巧
1、byte通常用來(lái)進(jìn)行位運算,位寬度窄,一般不用來(lái)計算
2、關(guān)鍵字都是小寫(xiě)的,在eclipse中顯示為紅色。
3、變量給了缺省的初始值,C語(yǔ)言沒(méi)給,只給分配了空間,里面的數不確定
4、char的缺省值是ASCII碼中第1個(gè)
5、運行效率:i++>i+=1>i=i+1
6、布爾值不能進(jìn)行大小比較,只能進(jìn)行==比較
7、先算&&再算||。另外&&為短路與的意思。
例1:判斷以下i的變化。
int i=2,j=3;
boolean b=i>j && i++>2;
System.out.println(i);
答案:2
例2:以下在a,b,i不知道的情況下,判斷真還是假。
((a>b)||((3-2)>2))||(5>2)&&(true||(++i>2))
答案:真
8、>>帶符號右移,前面移空的位置添加符號位相同的數
0|001 1000 右移兩位(正數)
0|000 0110
1|001 1000 右移兩位(負數)
1|111 1100
>>>帶符號右移,前面移空的位置添加0
9、獲得-5到2的隨機數
int i;
Random r=new Random();
i=r.nextInt();
// i=Math.abs(i%10)+1;//獲得0到10的隨機數
i=Math.abs(i%8)-5;//獲得-5到-2的隨機數
System.out.println(i);
10、數組創(chuàng )建時(shí),大小(內存)可以是前面的變量.可以動(dòng)態(tài)創(chuàng )建數組的大小(內存),創(chuàng )建后就不能再改大小.
例:
int t=9;
int[][] jiu;
jiu=new int[t][];
11、變量的作用域。
定義的數個(gè)變量其實(shí)是放在一個(gè)棧的結構中,后定義的變量先消失,先定義的變量后消失,作用域比后定義的變量大。
12、.基本數據類(lèi)型參數的傳遞是值傳遞,
引用....................址傳遞.
class Length{
int length;
}
class People{
void walk(Length length){
length.length=+=2;
}
public satic void main(String[] args){
Length l=new Length();
l.length=20;
new People().walk(l);
System.out.println(l.length);
}
}
13、方法的重載,不能通過(guò)返回值類(lèi)型不同來(lái)區別,只能通過(guò)參數的不同來(lái)區別.
14、方法或變量加static和
不加static的方法,是類(lèi)的對象的方法.對象消失,方法消失
加static的方法,是類(lèi)的方法.對象消失,方法存在.方法的地址是靜態(tài)的與類(lèi)綁定
變量和上面也一樣.
15、靜態(tài)方法,只能訪(fǎng)問(wèn)類(lèi)的靜態(tài)成員和該方法的成員
16、JAVA不支持多重繼承,類(lèi)似一個(gè)樹(shù),一個(gè)類(lèi)只有一個(gè)父類(lèi),但可以有多個(gè)接口
C++支持多重繼承,類(lèi)似一個(gè)網(wǎng),一個(gè)類(lèi)可以有多個(gè)父類(lèi)
17、子類(lèi)默認調用了父類(lèi)無(wú)參構造函數.如果父類(lèi)沒(méi)有無(wú)參構造函數,必須手動(dòng)重寫(xiě)子類(lèi)的構造方法,并用super(參數)調用父類(lèi)中有參構造的方法.
例:
class People{
private int age;
public People(int age){
this.age=age;
}
}
class Doctor extends People{
//不寫(xiě)以下構造方法則出錯.
public Doctor(){
super(23);
}
//或者
public Doctor(int age){
super(age);
}
}
解決方法:在寫(xiě)類(lèi)的有參構造方法時(shí),最好定義個(gè)無(wú)參的構造方法.
18、構造方法調用父類(lèi)的構造方法super()前不能有其他的語(yǔ)句.
19、final可以修飾形式參數,防止被改
例:
void walk(final int length){
}
20、import導入包的理解,重要的事情總要放到前面
21、private與abstract沖突,原因:子類(lèi)實(shí)現(繼承)不了接口(抽象類(lèi))中被private修飾的成員,但接口(抽象類(lèi))中的方法必須要重寫(xiě),加private就沒(méi)辦法重寫(xiě)了
例:
interface InterfaceTest {
int f();
private abstract int ff();//此句錯誤,要去掉private
}
22、內部類(lèi)可以被外部使用,可以訪(fǎng)問(wèn)外部類(lèi)(宿主類(lèi))的privite成員;內部類(lèi)成員加public也可以被外部訪(fǎng)問(wèn),但也危險,定義成private外部不能訪(fǎng)問(wèn)此類(lèi)了(常用).
public class OutClass {
private int i=2;
public class InnerClass{
public int j=i;
}
}
23、抽象類(lèi)不用繼承也能用
例:
abstract class Out2{
private int i=2;
public abstract int f();
public static Out2 getInstance(){
return new Inner2();
}
private static class Inner2 extends Out2{//static
public int f(){
return 2;
}
}
public static void main(String[] args) {
Out2 ou=Out2.getInstance();//抽象類(lèi)不用繼承也能用,獲得此類(lèi)的實(shí)例
int i=ou.f();
System.out.println(i);
}
}
24、接口里也可以定義類(lèi)(內隱類(lèi))
例:
interface InterfaceTest {
int f();
class A{
int f(){
return 2;
}
}
}
25、內部類(lèi)的使用.類(lèi)NoNameInnerClass 不用implements實(shí)現接口,而用傳遞進(jìn)來(lái)對象來(lái)用接口
interface Inter{
void paint();
}
public class NoNameInnerClass {
public void paint(Inter p){//傳遞進(jìn)來(lái)對象來(lái)用接口
p.paint();
}
public void func(){
//為了獲得對象,定義一個(gè)內部類(lèi),把此對象做參數
class Paint implements Inter{
public void paint(){
System.out.println("paint.");
}
}
Paint p=new Paint();
paint(p);
}
}
26、內部類(lèi)的使用.不用類(lèi)名,直接創(chuàng )建對象,無(wú)名內隱類(lèi),類(lèi)名是他實(shí)現的接口名字
interface Inter{
void paint();
}
public class NoNameInnerClass {
public void paint(Inter p){//傳遞進(jìn)來(lái)對象來(lái)用接口
p.paint();
}
public void func(){
//直接創(chuàng )建對象,無(wú)名內隱類(lèi),類(lèi)名是他實(shí)現的接口名字,
paint(new Inter(){
public void paint(){
}
});
}
}
27、單態(tài)設計模式。能夠創(chuàng )建類(lèi)的唯一實(shí)例。把構造方法定義成private,再創(chuàng )建靜態(tài)的成員方法getInstance()獲得此類(lèi)唯一實(shí)例.
例1.
public class ChineseTest{
public static void main(String[] args) {
Chinese Obj1 = Chinese.getInstance();
Chinese Obj2 = Chinese.getInstance();
System.out.println(Obj1 == Obj2);
}
}
class Chinese {
private static Chinese objRef = new Chinese();
private Chinese() {
}
public static Chinese getInstance(){
return objRef;
}
}
例2:
public class ChineseTest{
public static void main(String[] args) {
Chinese Obj1 = Chinese.getInstance();
Chinese Obj2 = Chinese.getInstance();
System.out.println(Obj1 == Obj2);
}
}
class Chinese {
private static Chinese objRef ;
private Chinese() {
}
}
28、泛型應用
Vector
例:
Vector
v.add("aaa");
v.add("bbb");
System.out.println(v.get(0));
29、如果一個(gè)方法可能有異常,則用throw new Exception("")來(lái)拋出異常
如果方法內用throw拋出異常,則此方法頭后必須用throws(多個(gè)s)聲明可能會(huì )拋出異常。
如果一個(gè)方法頭后用throws聲明可能會(huì )拋出異常,則此方法在用的時(shí)候必須用try-catch語(yǔ)句
例:
public class Lx_Yichang {
static int div(int x,int y)throws Exception{
if(y==0){
throw new Exception("除數不能為0!!!");//方法內用throw拋出異常
}else{
return x/y;
}
}
public static void main(String[] args) {
try{//用try-catch語(yǔ)句
int z=0;
z=Lx_Yichang.div(10, 0);
System.out.println(z);
}
catch(Exception ex){
System.out.println(ex.toString());
ex.printStackTrace();
}
finally{
System.out.println("End!");
}
}
}
30、Hashtable類(lèi)應用,可以通過(guò)get(鍵)或get(hashCode)來(lái)獲得值內容。
import java.util.Hashtable;
class PhoneList{
String name;
String phoneNumber;
public PhoneList(String name,String phoneNumber){
this.name=name;
this.phoneNumber=phoneNumber;
}
}
public class HashtableTest {
public static void main(String[] args) {
//利用泛型
Hashtable
hashTable.put("wang0",new PhoneList("wang","0000000"));
hashTable.put("wang1",new PhoneList("wang","1111111"));
hashTable.put("wang2",new PhoneList("wang","2222222"));
hashTable.put("wang3",new PhoneList("wang","3333333"));
System.out.println(hashTable.get("wang2").phoneNumber);
//不利用泛型
Hashtable hash=new Hashtable();
hash.put("wang0",new PhoneList("wang","0000000"));
hash.put("wang1",new PhoneList("wang","1111111"));
hash.put("wang2",new PhoneList("wang","2222222"));
hash.put("wang3",new PhoneList("wang","3333333"));
//System.out.println(((PhoneList)hash.get("wang2")).phoneNumber);//不用泛型,需要強制類(lèi)型轉換
//強制類(lèi)型轉換時(shí),最好先進(jìn)行類(lèi)型判斷
Object o=hash.get("wang2");
if(o instanceof PhoneList){
System.out.println(((PhoneList)o).phoneNumber);
}
//利用hashcode
Hashtable
PhoneList p1=new PhoneList("wang2","888888888888");
table.put(new Integer(p1.hashCode()), p1);
System.out.println(table.get(new Integer(p1.hashCode())).phoneNumber);
}
}
31、提供一個(gè)關(guān)于游戲的簡(jiǎn)短描述,網(wǎng)頁(yè),游戲名稱(chēng),提供商,金錢(qián)初始值等等。這些數據可以置于.jad文件中。 打包后是放在JAR的META-INF文件夾里。 用MIDlet類(lèi)的getAppProperty(String key)來(lái)獲得其中的值.
32、Canvas 的hideNotify() 里寫(xiě)暫停的代碼。showNotify() 寫(xiě)繼續游戲的代碼。
來(lái)電話(huà)時(shí)自動(dòng)調用hideNotify() 。 pauseApp也可以實(shí)現,但有的手機不支持如Nokia手機
34、運行提示ALERT: java/lang/ClassFormatError: Bad version information.原因
原因:當前編譯器的版本過(guò)高。
解決方法: 編譯器的版本不要過(guò)高否則有的手機不認,選擇編譯器方法:點(diǎn)項目右鍵屬性->Java Compiler-> Enable project specific settings打勾,然后選擇版本較低的編譯器
35、把所有的引用都設置為null才可以變?yōu)槔?/p>
Hero h=new Hero();
Hero h2=h;
h=null;//此時(shí),h并沒(méi)變?yōu)槔,因為還有h2指向它,需要把h2也設置為null,h才變?yōu)槔?/p>
h2=null;
36、去掉無(wú)用包(ctrl+shift+0).或右鍵->Source->Organize Imports
37、WTK2.5的安裝后,ECLIPSE的設置
Window->Preferences->Device Management->Import->找到WTK的安裝路徑
38、添加資源文件夾名
在項目上右鍵->Properties->雙擊Java Build Path->點(diǎn)Add Foler->在下面的選項中選擇update exclusion filters in other source folders to solve nesting,再添入資源文件夾名,如src、res等。
39、添加抽象類(lèi)、接口中的方法。
例如對繼承Canvas類(lèi),需要添加protected void keyPressed(int keyCode){}方法時(shí).
在代碼上右鍵->Source->選擇Override/Implements Methods->在窗口中,對要重寫(xiě)的方法打勾->Ok。
40、在for語(yǔ)句中,第2個(gè)循環(huán)條件可以和邏輯表達試取與
例:
for(int i=0; i<100 && i%2==0;i++)
41、DataInputStream包裝FileInputStream后,底層操作文件,上層按指定格式輸出(最易使用)
42、FileInputStream的應用
例:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class PekingFileInputStreamTest1 {
public static void main(String[] args) {
try {
//在項目根目錄里創(chuàng )建文件fileInputStream.txt
File file=new File("fileInputStream.txt");
FileOutputStream fout=new FileOutputStream(file);
fout.write(65);
fout.close();//輸出到文件完畢后關(guān)閉
//開(kāi)始讀
FileInputStream fin=new FileInputStream("fileInputStream.txt");
int b=fin.read();
System.out.println(b);
fin.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
43、利用DataOutputStream包裝FileInputStream按指定格式輸出
例:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
public class PekingFileInputStreamTest2 {
public static void main(String[] args) {
try {
//在項目根目錄里創(chuàng )建文件fileInputStream.txt
File file=new File("fileInputStream.txt");
FileOutputStream fout=new FileOutputStream(file);
//包裝下按指定格式輸出
DataOutputStream dout=new DataOutputStream(fout);//子類(lèi)fout做參數傳給父類(lèi),子類(lèi)當父類(lèi)用
dout.writeInt(8793);
dout.writeUTF("感動(dòng)中國");
dout.close();
fout.close();
//開(kāi)始讀
FileInputStream fin=new FileInputStream("fileInputStream.txt");
DataInputStream din=new DataInputStream(fin);
int b=din.readInt();
String s=din.readUTF();
System.out.println(b);
System.out.println(s);
din.close();
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
44、利用PrintWriter包裝三次的例子。
PrintWriter包裝OutputStreamWriter,OutputStreamWriter包裝FileOutputStream,FileOutputStream包裝File
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class PekingFileInputStreamTest3 {
public static void main(String[] args) {
try {
//在項目根目錄里創(chuàng )建文件fileInputStream.txt
File file=new File("fileInputStream.txt");
// FileOutputStream fout=new FileOutputStream(file);
// OutputStreamWriter osw=new OutputStreamWriter(fout);//測試字符流//字符流通向字節流的橋梁
// PrintWriter pw=new PrintWriter(osw);//包裝三次
PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file)));
//開(kāi)始讀
pw.println("窗前名月光,");
pw.println("疑是地上霜.");
pw.println("抬頭望明月,");
pw.println("低頭思故鄉。");
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
【java學(xué)習技巧】相關(guān)文章:
學(xué)習java技巧09-23
學(xué)習Java的技巧07-30
學(xué)習Java的6個(gè)技巧07-13
Java基本編程技巧07-13
Java中類(lèi)的設計技巧有哪些10-21
JAVA和WAP移動(dòng)學(xué)習技術(shù)07-28
Java程序員必知調試技巧匯總08-11
Sun認證Java程序員考試技巧分享08-06
學(xué)習古箏的技巧08-14