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();
}
}
}