java學習技巧
1、byte通常用來進行位運算,位寬度窄,一般不用來計算
2、關鍵字都是小寫的,在eclipse中顯示為紅色。
3、變數給了預設的初始值,C語言沒給,只給分配了空間,裡面的數不確定
4、char的預設值是ASCII碼中第1個
5、執行效率:i++>i+=1>i=i+1
6、布林值不能進行大小比較,只能進行==比較
7、先算&&再算||。另外&&為短路與的意思。
例1:判斷以下i的變化。
int i=2,j=3;
boolean b=i>j && i++>2;
tln(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=Int();
// i=(i%10)+1;//獲得0到10的隨機數
i=(i%8)-5;//獲得-5到-2的隨機數
tln(i);
10、陣列建立時,大小(記憶體)可以是前面的變數.可以動態建立陣列的大小(記憶體),建立後就不能再改大小.
例:
int t=9;
int[][] jiu;
jiu=new int[t][];
11、變數的作用域。
定義的數個變數其實是放在一個棧的結構中,後定義的變數先消失,先定義的變數後消失,作用域比後定義的變數大。
12、.基本資料型別引數的傳遞是值傳遞,
引用....................址傳遞.
class Length{
int length;
}
class People{
void walk(Length length){
th=+=2;
}
public satic void main(String[] args){
Length l=new Length();
th=20;
new People()(l);
tln(th);
}
}
13、方法的過載,不能通過返回值型別不同來區別,只能通過引數的不同來區別.
14、方法或變數加static和
不加static的方法,是類的物件的方法.物件消失,方法消失
加static的方法,是類的方法.物件消失,方法存在.方法的地址是靜態的與類繫結
變數和上面也一樣.
15、靜態方法,只能訪問類的'靜態成員和該方法的成員
16、JAVA不支援多重繼承,類似一個樹,一個類只有一個父類,但可以有多個介面
C++支援多重繼承,類似一個網,一個類可以有多個父類
17、子類預設呼叫了父類無參建構函式.如果父類沒有無參建構函式,必須手動重寫子類的構造方法,並用super(引數)呼叫父類中有參構造的方法.
例:
class People{
private int age;
public People(int age){
=age;
}
}
class Doctor extends People{
//不寫以下構造方法則出錯.
public Doctor(){
super(23);
}
//或者
public Doctor(int age){
super(age);
}
}
解決方法:在寫類的有參構造方法時,最好定義個無參的構造方法.
18、構造方法呼叫父類的構造方法super()前不能有其他的語句.
19、final可以修飾形式引數,防止被改
例:
void walk(final int length){
}
20、import匯入包的理解,重要的事情總要放到前面
21、private與abstract衝突,原因:子類實現(繼承)不了介面(抽象類)中被private修飾的成員,但介面(抽象類)中的方法必須要重寫,加private就沒辦法重寫了
例:
interface InterfaceTest {
int f();
private abstract int ff();//此句錯誤,要去掉private
}
22、內部類可以被外部使用,可以訪問外部類(宿主類)的privite成員;內部類成員加public也可以被外部訪問,但也危險,定義成private外部不能訪問此類了(常用).
public class OutClass {
private int i=2;
public class InnerClass{
public int j=i;
}
}
23、抽象類不用繼承也能用
例:
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=nstance();//抽象類不用繼承也能用,獲得此類的例項
int i=ou.f();
tln(i);
}
}
24、接口裡也可以定義類(內隱類)
例:
interface InterfaceTest {
int f();
class A{
int f(){
return 2;
}
}
}
25、內部類的使用.類NoNameInnerClass 不用implements實現介面,而用傳遞進來物件來用介面
interface Inter{
void paint();
}
public class NoNameInnerClass {
public void paint(Inter p){//傳遞進來物件來用介面
t();
}
public void func(){
//為了獲得物件,定義一個內部類,把此物件做引數
class Paint implements Inter{
public void paint(){
tln("paint.");
}
}
Paint p=new Paint();
paint(p);
}
}
26、內部類的使用.不用類名,直接建立物件,無名內隱類,類名是他實現的介面名字
interface Inter{
void paint();
}
public class NoNameInnerClass {
public void paint(Inter p){//傳遞進來物件來用介面
t();
}
public void func(){
//直接建立物件,無名內隱類,類名是他實現的介面名字,
paint(new Inter(){
public void paint(){
}
});
}
}
27、單態設計模式。能夠建立類的唯一例項。把構造方法定義成private,再建立靜態的成員方法getInstance()獲得此類唯一例項.
例1.
public class ChineseTest{
public static void main(String[] args) {
Chinese Obj1 = nstance();
Chinese Obj2 = nstance();
tln(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 = nstance();
Chinese Obj2 = nstance();
tln(Obj1 == Obj2);
}
}
class Chinese {
private static Chinese objRef ;
private Chinese() {
}
}
28、泛型應用
Vector
例:
Vector
("aaa");
("bbb");
tln((0));
29、如果一個方法可能有異常,則用throw new Exception("")來丟擲異常
如果方法內用throw丟擲異常,則此方法頭後必須用throws(多個s)宣告可能會丟擲異常。
如果一個方法頭後用throws宣告可能會丟擲異常,則此方法在用的時候必須用try-catch語句
例:
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語句
int z=0;
z=Lx_(10, 0);
tln(z);
}
catch(Exception ex){
tln(ring());
tStackTrace();
}
finally{
tln("End!");
}
}
}
30、Hashtable類應用,可以通過get(鍵)或get(hashCode)來獲得值內容。
import table;
class PhoneList{
String name;
String phoneNumber;
public PhoneList(String name,String phoneNumber){
=name;
eNumber=phoneNumber;
}
}
public class HashtableTest {
public static void main(String[] args) {
//利用泛型
Hashtable
("wang0",new PhoneList("wang","0000000"));
("wang1",new PhoneList("wang","1111111"));
("wang2",new PhoneList("wang","2222222"));
("wang3",new PhoneList("wang","3333333"));
tln(("wang2")eNumber);
//不利用泛型
Hashtable hash=new Hashtable();
("wang0",new PhoneList("wang","0000000"));
("wang1",new PhoneList("wang","1111111"));
("wang2",new PhoneList("wang","2222222"));
("wang3",new PhoneList("wang","3333333"));
//tln(((PhoneList)("wang2"))eNumber);//不用泛型,需要強制型別轉換
//強制型別轉換時,最好先進行型別判斷
Object o=("wang2");
if(o instanceof PhoneList){
tln(((PhoneList)o)eNumber);
}
//利用hashcode
Hashtable
PhoneList p1=new PhoneList("wang2","888888888888");
(new Integer(Code()), p1);
tln((new Integer(Code()))eNumber);
}
}
31、提供一個關於遊戲的簡短描述,網頁,遊戲名稱,提供商,金錢初始值等等。這些資料可以置於檔案中。 打包後是放在JAR的META-INF資料夾裡。 用MIDlet類的getAppProperty(String key)來獲得其中的值.
32、Canvas 的hideNotify() 裡寫暫停的程式碼。showNotify() 寫繼續遊戲的程式碼。
來電話時自動呼叫hideNotify() 。 pauseApp也可以實現,但有的手機不支援如Nokia手機
34、執行提示ALERT: java/lang/ClassFormatError: Bad version information.原因
原因:當前編譯器的版本過高。
解決方法: 編譯器的版本不要過高否則有的手機不認,選擇編譯器方法:點專案右鍵屬性->Java Compiler-> Enable project specific settings打勾,然後選擇版本較低的編譯器
35、把所有的引用都設定為null才可以變為垃圾
Hero h=new Hero();
Hero h2=h;
h=null;//此時,h並沒變為垃圾,因為還有h2指向它,需要把h2也設定為null,h才變為垃圾
h2=null;
36、去掉無用包(ctrl+shift+0).或右鍵->Source->Organize Imports
37、WTK2.5的安裝後,ECLIPSE的設定
Window->Preferences->Device Management->Import->找到WTK的安裝路徑
38、新增資原始檔夾名
在專案上右鍵->Properties->雙擊Java Build Path->點Add Foler->在下面的選項中選擇update exclusion filters in other source folders to solve nesting,再添入資原始檔夾名,如src、res等。
39、新增抽象類、介面中的方法。
例如對繼承Canvas類,需要新增protected void keyPressed(int keyCode){}方法時.
在程式碼上右鍵->Source->選擇Override/Implements Methods->在視窗中,對要重寫的方法打勾->Ok。
40、在for語句中,第2個迴圈條件可以和邏輯表達試取與
例:
for(int i=0; i<100 && i%2==0;i++)
41、DataInputStream包裝FileInputStream後,底層操作文件,上層按指定格式輸出(最易使用)
42、FileInputStream的應用
例:
import ;
import InputStream;
import OutputStream;
public class PekingFileInputStreamTest1 {
public static void main(String[] args) {
try {
//在專案根目錄裡建立檔案
File file=new File("");
FileOutputStream fout=new FileOutputStream(file);
e(65);
e();//輸出到檔案完畢後關閉
//開始讀
FileInputStream fin=new FileInputStream("");
int b=();
tln(b);
e();
} catch (Exception e) {
// TODO: handle exception
tStackTrace();
}
}
}
43、利用DataOutputStream包裝FileInputStream按指定格式輸出
例:
import InputStream;
import OutputStream;
import ;
import OutputStream;
import InputStream;
public class PekingFileInputStreamTest2 {
public static void main(String[] args) {
try {
//在專案根目錄裡建立檔案
File file=new File("");
FileOutputStream fout=new FileOutputStream(file);
//包裝下按指定格式輸出
DataOutputStream dout=new DataOutputStream(fout);//子類fout做引數傳給父類,子類當父類用
eInt(8793);
eUTF("感動中國");
e();
e();
//開始讀
FileInputStream fin=new FileInputStream("");
DataInputStream din=new DataInputStream(fin);
int b=Int();
String s=UTF();
tln(b);
tln(s);
e();
e();
} catch (Exception e) {
tStackTrace();
}
}
}
44、利用PrintWriter包裝三次的例子。
PrintWriter包裝OutputStreamWriter,OutputStreamWriter包裝FileOutputStream,FileOutputStream包裝File
import ;
import OutputStream;
import utStreamWriter;
import tWriter;
public class PekingFileInputStreamTest3 {
public static void main(String[] args) {
try {
//在專案根目錄裡建立檔案
File file=new File("");
// FileOutputStream fout=new FileOutputStream(file);
// OutputStreamWriter osw=new OutputStreamWriter(fout);//測試字元流//字元流通向位元組流的橋樑
// PrintWriter pw=new PrintWriter(osw);//包裝三次
PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file)));
//開始讀
tln("窗前名月光,");
tln("疑是地上霜.");
tln("抬頭望明月,");
tln("低頭思故鄉。");
h();
} catch (Exception e) {
tStackTrace();
}
}
}