<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>

            java學習技巧

            時間:2024-09-13 04:26:30 JAVA認證 我要投稿

            java學習技巧

              1、byte通常用來進行位運算,位寬度窄,一般不用來計算

            java學習技巧

              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;

              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、數組創建時,大小(內存)可以是前面的變量.可以動態創建數組的大小(內存),創建后就不能再改大小.

              例:

              int t=9;

              int[][] jiu;

              jiu=new int[t][];

              11、變量的作用域。

              定義的數個變量其實是放在一個棧的結構中,后定義的變量先消失,先定義的變量后消失,作用域比后定義的變量大。

              12、.基本數據類型參數的傳遞是值傳遞,

              引用....................址傳遞.

              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、方法的重載,不能通過返回值類型不同來區別,只能通過參數的不同來區別.

              14、方法或變量加static和

              不加static的方法,是類的對象的方法.對象消失,方法消失

              加static的方法,是類的方法.對象消失,方法存在.方法的地址是靜態的與類綁定

              變量和上面也一樣.

              15、靜態方法,只能訪問類的靜態成員和該方法的成員

              16、JAVA不支持多重繼承,類似一個樹,一個類只有一個父類,但可以有多個接口

              C++支持多重繼承,類似一個網,一個類可以有多個父類

              17、子類默認調用了父類無參構造函數.如果父類沒有無參構造函數,必須手動重寫子類的構造方法,并用super(參數)調用父類中有參構造的方法.

              例:

              class People{

              private int age;

              public People(int age){

              this.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=Out2.getInstance();//抽象類不用繼承也能用,獲得此類的實例

              int i=ou.f();

              System.out.println(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){//傳遞進來對象來用接口

              p.paint();

              }

              public void func(){

              //為了獲得對象,定義一個內部類,把此對象做參數

              class Paint implements Inter{

              public void paint(){

              System.out.println("paint.");

              }

              }

              Paint p=new Paint();

              paint(p);

              }

              }

              26、內部類的使用.不用類名,直接創建對象,無名內隱類,類名是他實現的接口名字

              interface Inter{

              void paint();

              }

              public class NoNameInnerClass {

              public void paint(Inter p){//傳遞進來對象來用接口

              p.paint();

              }

              public void func(){

              //直接創建對象,無名內隱類,類名是他實現的接口名字,

              paint(new Inter(){

              public void paint(){

              }

              });

              }

              }

              27、單態設計模式。能夠創建類的唯一實例。把構造方法定義成private,再創建靜態的成員方法getInstance()獲得此類唯一實例.

              例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類后的E代表泛型,用來限制往里面放的數據類型

              例:

              Vector v=new Vector();

              v.add("aaa");

              v.add("bbb");

              System.out.println(v.get(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_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類應用,可以通過get(鍵)或get(hashCode)來獲得值內容。

              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=new 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);//不用泛型,需要強制類型轉換

              //強制類型轉換時,最好先進行類型判斷

              Object o=hash.get("wang2");

              if(o instanceof PhoneList){

              System.out.println(((PhoneList)o).phoneNumber);

              }

              //利用hashcode

              Hashtable table=new 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、提供一個關于游戲的簡短描述,網頁,游戲名稱,提供商,金錢初始值等等。這些數據可以置于.jad文件中。 打包后是放在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 java.io.File;

              import java.io.FileInputStream;

              import java.io.FileOutputStream;

              public class PekingFileInputStreamTest1 {

              public static void main(String[] args) {

              try {

              //在項目根目錄里創建文件fileInputStream.txt

              File file=new File("fileInputStream.txt");

              FileOutputStream fout=new FileOutputStream(file);

              fout.write(65);

              fout.close();//輸出到文件完畢后關閉

              //開始讀

              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 {

              //在項目根目錄里創建文件fileInputStream.txt

              File file=new File("fileInputStream.txt");

              FileOutputStream fout=new FileOutputStream(file);

              //包裝下按指定格式輸出

              DataOutputStream dout=new DataOutputStream(fout);//子類fout做參數傳給父類,子類當父類用

              dout.writeInt(8793);

              dout.writeUTF("感動中國");

              dout.close();

              fout.close();

              //開始讀

              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 {

              //在項目根目錄里創建文件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)));

              //開始讀

              pw.println("窗前名月光,");

              pw.println("疑是地上霜.");

              pw.println("抬頭望明月,");

              pw.println("低頭思故鄉。");

              pw.flush();

              } catch (Exception e) {

              e.printStackTrace();

              }

              }

              }

            【java學習技巧】相關文章:

            學習java技巧09-23

            學習Java的技巧07-30

            學習Java的6個技巧07-13

            Java基本編程技巧07-13

            Java中類的設計技巧有哪些10-21

            JAVA和WAP移動學習技術07-28

            sun認證java關于字符串處理技巧06-01

            Java程序員必知調試技巧匯總08-11

            Sun認證Java程序員考試技巧分享08-06

            學習古箏的技巧08-14

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院