<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • java學(xué)習(xí)技巧

    時間:2024-09-13 04:26:30 JAVA認(rèn)證 我要投稿

    java學(xué)習(xí)技巧

      1、byte通常用來進(jìn)行位運(yùn)算,位寬度窄,一般不用來計算

    java學(xué)習(xí)技巧

      2、關(guān)鍵字都是小寫的,在eclipse中顯示為紅色。

      3、變量給了缺省的初始值,C語言沒給,只給分配了空間,里面的數(shù)不確定

      4、char的缺省值是ASCII碼中第1個

      5、運(yùn)行效率: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、>>帶符號右移,前面移空的位置添加符號位相同的數(shù)

      0|001 1000 右移兩位(正數(shù))

      0|000 0110

      1|001 1000 右移兩位(負(fù)數(shù))

      1|111 1100

      >>>帶符號右移,前面移空的位置添加0

      9、獲得-5到2的隨機(jī)數(shù)

      int i;

      Random r=new Random();

      i=r.nextInt();

      // i=Math.abs(i%10)+1;//獲得0到10的隨機(jī)數(shù)

      i=Math.abs(i%8)-5;//獲得-5到-2的隨機(jī)數(shù)

      System.out.println(i);

      10、數(shù)組創(chuàng)建時,大小(內(nèi)存)可以是前面的變量.可以動態(tài)創(chuàng)建數(shù)組的大小(內(nèi)存),創(chuàng)建后就不能再改大小.

      例:

      int t=9;

      int[][] jiu;

      jiu=new int[t][];

      11、變量的作用域。

      定義的數(shù)個變量其實(shí)是放在一個棧的結(jié)構(gòu)中,后定義的變量先消失,先定義的變量后消失,作用域比后定義的變量大。

      12、.基本數(shù)據(jù)類型參數(shù)的傳遞是值傳遞,

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

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

      14、方法或變量加static和

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

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

      變量和上面也一樣.

      15、靜態(tài)方法,只能訪問類的靜態(tài)成員和該方法的成員

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

      C++支持多重繼承,類似一個網(wǎng),一個類可以有多個父類

      17、子類默認(rèn)調(diào)用了父類無參構(gòu)造函數(shù).如果父類沒有無參構(gòu)造函數(shù),必須手動重寫子類的構(gòu)造方法,并用super(參數(shù))調(diào)用父類中有參構(gòu)造的方法.

      例:

      class People{

      private int age;

      public People(int age){

      this.age=age;

      }

      }

      class Doctor extends People{

      //不寫以下構(gòu)造方法則出錯.

      public Doctor(){

      super(23);

      }

      //或者

      public Doctor(int age){

      super(age);

      }

      }

      解決方法:在寫類的有參構(gòu)造方法時,最好定義個無參的構(gòu)造方法.

      18、構(gòu)造方法調(diào)用父類的構(gòu)造方法super()前不能有其他的語句.

      19、final可以修飾形式參數(shù),防止被改

      例:

      void walk(final int length){

      }

      20、import導(dǎo)入包的理解,重要的事情總要放到前面

      21、private與abstract沖突,原因:子類實(shí)現(xiàn)(繼承)不了接口(抽象類)中被private修飾的成員,但接口(抽象類)中的方法必須要重寫,加private就沒辦法重寫了

      例:

      interface InterfaceTest {

      int f();

      private abstract int ff();//此句錯誤,要去掉private

      }

      22、內(nèi)部類可以被外部使用,可以訪問外部類(宿主類)的privite成員;內(nèi)部類成員加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();//抽象類不用繼承也能用,獲得此類的實(shí)例

      int i=ou.f();

      System.out.println(i);

      }

      }

      24、接口里也可以定義類(內(nèi)隱類)

      例:

      interface InterfaceTest {

      int f();

      class A{

      int f(){

      return 2;

      }

      }

      }

      25、內(nèi)部類的使用.類NoNameInnerClass 不用implements實(shí)現(xiàn)接口,而用傳遞進(jìn)來對象來用接口

      interface Inter{

      void paint();

      }

      public class NoNameInnerClass {

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

      p.paint();

      }

      public void func(){

      //為了獲得對象,定義一個內(nèi)部類,把此對象做參數(shù)

      class Paint implements Inter{

      public void paint(){

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

      }

      }

      Paint p=new Paint();

      paint(p);

      }

      }

      26、內(nèi)部類的使用.不用類名,直接創(chuàng)建對象,無名內(nèi)隱類,類名是他實(shí)現(xiàn)的接口名字

      interface Inter{

      void paint();

      }

      public class NoNameInnerClass {

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

      p.paint();

      }

      public void func(){

      //直接創(chuàng)建對象,無名內(nèi)隱類,類名是他實(shí)現(xiàn)的接口名字,

      paint(new Inter(){

      public void paint(){

      }

      });

      }

      }

      27、單態(tài)設(shè)計模式。能夠創(chuàng)建類的唯一實(shí)例。把構(gòu)造方法定義成private,再創(chuàng)建靜態(tài)的成員方法getInstance()獲得此類唯一實(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、泛型應(yīng)用

      Vector類后的E代表泛型,用來限制往里面放的數(shù)據(jù)類型

      例:

      Vector v=new Vector();

      v.add("aaa");

      v.add("bbb");

      System.out.println(v.get(0));

      29、如果一個方法可能有異常,則用throw new Exception("")來拋出異常

      如果方法內(nèi)用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("除數(shù)不能為0!!!");//方法內(nèi)用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類應(yīng)用,可以通過get(鍵)或get(hashCode)來獲得值內(nè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=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);//不用泛型,需要強(qiáng)制類型轉(zhuǎn)換

      //強(qiáng)制類型轉(zhuǎn)換時,最好先進(jìn)行類型判斷

      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、提供一個關(guān)于游戲的簡短描述,網(wǎng)頁,游戲名稱,提供商,金錢初始值等等。這些數(shù)據(jù)可以置于.jad文件中。 打包后是放在JAR的META-INF文件夾里。 用MIDlet類的getAppProperty(String key)來獲得其中的值.

      32、Canvas 的hideNotify() 里寫暫停的代碼。showNotify() 寫繼續(xù)游戲的代碼。

      來電話時自動調(diào)用hideNotify() 。 pauseApp也可以實(shí)現(xiàn),但有的手機(jī)不支持如Nokia手機(jī)

      34、運(yùn)行提示ALERT: java/lang/ClassFormatError: Bad version information.原因

      原因:當(dāng)前編譯器的版本過高。

      解決方法: 編譯器的版本不要過高否則有的手機(jī)不認(rèn),選擇編譯器方法:點(diǎn)項(xiàng)目右鍵屬性->Java Compiler-> Enable project specific settings打勾,然后選擇版本較低的編譯器

      35、把所有的引用都設(shè)置為null才可以變?yōu)槔?/p>

      Hero h=new Hero();

      Hero h2=h;

      h=null;//此時,h并沒變?yōu)槔驗(yàn)檫有h2指向它,需要把h2也設(shè)置為null,h才變?yōu)槔?/p>

      h2=null;

      36、去掉無用包(ctrl+shift+0).或右鍵->Source->Organize Imports

      37、WTK2.5的安裝后,ECLIPSE的設(shè)置

      Window->Preferences->Device Management->Import->找到WTK的安裝路徑

      38、添加資源文件夾名

      在項(xiàng)目上右鍵->Properties->雙擊Java Build Path->點(diǎn)Add Foler->在下面的選項(xiàng)中選擇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個循環(huán)條件可以和邏輯表達(dá)試取與

      例:

      for(int i=0; i<100 && i%2==0;i++)

      41、DataInputStream包裝FileInputStream后,底層操作文件,上層按指定格式輸出(最易使用)

      42、FileInputStream的應(yīng)用

      例:

      import java.io.File;

      import java.io.FileInputStream;

      import java.io.FileOutputStream;

      public class PekingFileInputStreamTest1 {

      public static void main(String[] args) {

      try {

      //在項(xiàng)目根目錄里創(chuàng)建文件fileInputStream.txt

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

      FileOutputStream fout=new FileOutputStream(file);

      fout.write(65);

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

      //開始讀

      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 {

      //在項(xiàng)目根目錄里創(chuàng)建文件fileInputStream.txt

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

      FileOutputStream fout=new FileOutputStream(file);

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

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

      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,F(xiàn)ileOutputStream包裝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 {

      //在項(xiàng)目根目錄里創(chuàng)建文件fileInputStream.txt

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

      // FileOutputStream fout=new FileOutputStream(file);

      // OutputStreamWriter osw=new OutputStreamWriter(fout);//測試字符流//字符流通向字節(jié)流的橋梁

      // PrintWriter pw=new PrintWriter(osw);//包裝三次

      PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file)));

      //開始讀

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

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

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

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

      pw.flush();

      } catch (Exception e) {

      e.printStackTrace();

      }

      }

      }

    【java學(xué)習(xí)技巧】相關(guān)文章:

    學(xué)習(xí)java技巧09-23

    學(xué)習(xí)Java的技巧07-30

    學(xué)習(xí)Java的6個技巧07-13

    Java基本編程技巧07-13

    Java中類的設(shè)計技巧有哪些10-21

    JAVA和WAP移動學(xué)習(xí)技術(shù)07-28

    sun認(rèn)證java關(guān)于字符串處理技巧06-01

    Java程序員必知調(diào)試技巧匯總08-11

    Sun認(rèn)證Java程序員考試技巧分享08-06

    學(xué)習(xí)古箏的技巧08-14

    主站蜘蛛池模板: 亚洲一区爱区精品无码 | 久久精品不卡| 久久亚洲欧美国产精品| 精品无码久久久久久国产| 国产精品久久久久影视不卡| 日韩精品无码中文字幕一区二区 | 精品无码久久久久久久动漫| 亚洲国产精品第一区二区| 国内精品人妻无码久久久影院| 亚洲国产精品无码久久青草 | 98视频精品全部国产| 日本精品久久久久影院日本| 亚洲综合国产精品| 国产成人精品日本亚洲11| 久久久久女人精品毛片| 午夜在线视频91精品| 国产亚洲美女精品久久久| 中文字幕一区二区精品区| 国产精品久久久久影院色| 国产精品区免费视频| 久久香蕉国产线看观看精品yw| 亚洲?V乱码久久精品蜜桃| 久久99热这里只有精品国产| 国产精品香蕉在线观看| 99久久精品国产一区二区三区 | 欧美成人精品一区二区综合| 国产成人综合久久精品尤物| 四虎国产精品免费观看| 精品久久一区二区三区| 国产精品久久久久久久久鸭 | 99久久精品毛片免费播放| 国产精品视频白浆免费视频 | 99精品视频在线观看re| 国产精品福利一区二区| 精品亚洲一区二区| 911亚洲精品国产自产| 99久久精品九九亚洲精品| 国产精品热久久无码av| 精品人妻伦九区久久AAA片69| 久久久精品日本一区二区三区 | 国产精品你懂得|