Java程式設計—孫鑫java無難事教程Lesson1《java技術與應用》

來源:互聯網
上載者:User
Java程式設計—孫鑫java無難事教程Lesson1《java技術與應用》

1.Java語言特性
簡單的、物件導向的、健壯的、安全的、解釋的、與平台無關的、多線程的、動態語言的。
2.Java跨平台原理
原理介紹如所示:
圖1:

圖2:

3.java環境熟悉
(1)path環境變數
啟動cmd.exe後,鍵入set path查看路徑,如下:
C:\Users\juber>set path
Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32
\WindowsPowerShell\v1.0\;C:\Program Files\Common Files\Thunder Network\KanKan\Co
decs;C:\Program Files\Common Files\Compuware;C:\Program Files\Java\jdk1.7.0_03\b
in
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
配置環境變數如所示:

(2)jar命令
a.打包檔案
F:\java\JavaLesson\Lesson1>jar cvf Lesson1.jar Hello.class Welcome.class
//**********************************************
b.整個目錄打包
F:\java\JavaLesson>jar cvf Lesson1.jar Lesson1/
已添加清單
正在添加: Lesson1/(輸入 = 0) (輸出 = 0)(儲存了 0%)
正在添加: Lesson1/Hello.class(輸入 = 416) (輸出 = 286)(壓縮了 31%)
正在添加: Lesson1/Hello.java(輸入 = 169) (輸出 = 127)(壓縮了 24%)
...
(3)public類名和檔案名稱相同
4.Java的8種資料類型
byte b;//1個位元組有符號整數
short s;//2位元組有符號整型
int   i;//4位元組有符號整型
long   l;//8位元組有符號整型
char   c;//2位元組無符號整型65535種字元 採用UNICODE編碼
float  f;//4位元組浮點數
double d;//8位元組浮點數
boolean bool=true;//兩種值false和true
5.Java資料類型及文法熟悉
//**********************************************
//測試代碼如下
public class   Hello{    public static void main(String[] args)    {        //System.out.println("Hello World!");        /*        byte b;//1個位元組有符號整數        b=3;        b=(byte)(b*3); //ok        //b=b*3;//error int to byte possible lose of data        System.out.println(b);        */        /*        short s;//2位元組有符號整型        int   i;//4位元組有符號整型        long   l;//8位元組有符號整型        char   c;//2位元組無符號整型65535種字元 採用UNICODE編碼        */        /*        float  f;//4位元組浮點數        f=1.3f;//ok        //f=1.3;//error possible lose of data        System.out.println(f);        */        /*        double d;//8位元組浮點數        boolean bool=true;//ok        //boolean bool=1 ;//error 錯誤  不能與整型進行相互轉換        System.out.println(bool);        */        /*        //error          if(1)        {                }        //ok        if(true)        {        }        */        /*        //數組定義        //int num[3];//error 定義時不能分配空間        //int num1[];        //num1=new int[3];//ok 定以後分配        //定義時分配        //int[] num2=new int[]{1,2,3};//ok        //int[] num2=new int[3]{1,2,3};//error 加上大小錯誤        //簡單的初始化形式        int[] num3=new int[3];        num3[0]=1;        num3[1]=2;        num3[2]=3;        */        /*        //二維數組        int[][] num4;        num4=new int[3][4];        num4[0][1]=2;        num4[0][2]=3;        */        /*        //不規則數組        int[][] num5;        num5=new int[3][];        num5[0]=new int[5];        num5[1]=new int[3];        num5[2]=new int[2];        num5[0][4]=45;        num5[1][2]=34;        num5[2][1]=21;        System.out.println(num5[0][4]);        System.out.println(num5[1][2]);        System.out.println(num5[2][1]);        //System.out.println(num5[2][2]);//error 數組越界        */                //二維數組初始化        //int[][] num6=new int[][]{1,2,3,4,5,6};//error        //int[][] num6=new int[][]{{1,2,3},{4,5,6}};//ok        //int[][] num6=new int[2][3]{{1,2,3},{4,5,6}};//error        //int[][] num6={{1,2,3},{4,5,6}};//ok        //*/        /*        //不規則數組初始化        int[][] num7={{1,2,3},{4,5},{6}};        System.out.println(num7[0][2]);        System.out.println(num7[1][1]);        System.out.println(num7[2][0]);        //System.out.println(num7[2][2]);//error 數組越界        */        /*        //++操作依賴於具體語言        //++測試1        int i=3;        //System.out.println(i++);//result 3        //System.out.println(++i);//result 4        int count=(i++)+(i++)+(i++);        System.out.println(i);//result 6        System.out.println(count);// result 12        //++測試2        int j=3;        int count2=(++j)+(++j)+(++j);        System.out.println(j);//result 6        System.out.println(count2);// result 15        */    }}

//相應的錯誤類型及結果如下所示:
//**********************************************
(1)缺少main函數
//**********************************************
錯誤: 在類 Hello 中找不到主方法, 請將主方法定義為:
  public static void main(String[] args)
(2)設定了錯誤的類路徑
//**********************************************
F:\java\JavaLesson\Lesson1>set classpath=e:\
F:\java\JavaLesson\Lesson1>java Hello
錯誤: 找不到或無法載入主類 Hello
(3)類型不符
//**********************************************
Hello.java:8: 錯誤: 可能損失精度
                b=b*3;
                   ^
  需要: byte
  找到:    int
1 個錯誤
(4)精度不匹配
//**********************************************
Hello.java:19: 錯誤: 可能損失精度
                f=1.3;
                  ^
  需要: float
  找到:    double
1 個錯誤
(5)布爾類型賦值不相容
//**********************************************
Hello.java:24: 錯誤: 不相容的類型
        boolean bool=1;
                     ^
  需要: boolean
  找到:    int
1 個錯誤
(6)布林運算式中類型不相容
//**********************************************
if(1)
{    
}
Hello.java:28: 錯誤: 不相容的類型
        if(1)
           ^
  需要: boolean
  找到:    int
1 個錯誤
(7)數組定義錯誤1   int num[3];//定義形式不能類似c語言
//**********************************************
Hello.java:35: 錯誤: 需要']'
        int num[3];//定義時不能分配空間
                ^
Hello.java:35: 錯誤: 非法的運算式開始
        int num[3];//定義時不能分配空間
                 ^
2 個錯誤
(8)數組定義錯誤2 加上定義列表時無需指定大小
Hello.java:39: 錯誤: 需要';'
                int num2[]=new int[3]{1,2,3};//加上大小錯誤
                                     ^
1 個錯誤
(9)數組定義錯誤3  越界錯誤
//**********************************************
45
34
21
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
        at Hello.main(Hello.java:71)
(10)數組定義錯誤4  數組元素初始化錯誤
//**********************************************
Hello.java:79: 錯誤: 不相容的類型
                int[][] num6=new int[][]{1,2,3,4,5,6};//error
                                         ^
  需要: int[]
  找到:    int
Hello.java:79: 錯誤: 不相容的類型
                int[][] num6=new int[][]{1,2,3,4,5,6};//error
                                           ^
...
(11)數組定義錯誤5  數組元素初始化錯誤 不能指定數組大小
//**********************************************
Hello.java:81: 錯誤: 需要';'
        int[][] num6=new int[2][3]{{1,2,3},{4,5,6}};//error
                                  ^
Hello.java:81: 錯誤: 不是語句
        int[][] num6=new int[2][3]{{1,2,3},{4,5,6}};//error
... 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.