Java語言基本語法
一、標識符和關鍵字
- 標識符
- 在java語言中,用來標志類名、對象名、變量名、方法名、類型名、數組名、包名的有效字符序列,稱為“標識符”;
- 標識符由字母、數字、下划線、美元符號組成,且第一個字符不能是數字;
- java語言區分大小寫;
- 標志符命名規則:類名首字母大寫,變量名和方法名采用駝峰標志法,常量全大寫,多個單詞之間用“_”隔開,包名全小寫;
- 關鍵字
- 在java語言中,有一些專門的詞匯已經被賦予了特殊的含義,不能再使用這些詞匯來命名標識符,這些專有詞匯,稱為“關鍵字”;
- java有50個關鍵字和3個保留字,均不能用來命名標識符;
abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while - true、false、null不是關鍵字,是保留字,但是仍不能用來命名標識符,保留字是java預留的關鍵字,在以后的升級版本中可能會作為關鍵字;
二、基本數據類型
1. 整數類型(int為默認類型)
2. 浮點類型(double為默認類型)
-
- 在給float類型的變量賦值時,如果賦的值有小數部分則一定要在末尾加上“F”或“f”;
3. 字符類型(2字節)
-
- char ch = 'a';
- 有些字符不能通過鍵盤輸入到程序當中,這時就需要使用到轉義字符;
4. 布爾類型(1字節)
-
- boolean flag = true;
5. 默認值
-
- 數值變量:0;
- 字符變量:‘\0’;
- 布爾變量:false;
- 引用數據類型:null;
6. 不同數據類型之間的轉換
-
- 自動類型轉換(低 到 高)
-
- 強制類型轉換(高 到 低)
public class Test003 { public static void main(String[] args) { byte b = 100; int i = 22; float f = 78.98f; int res = b + i + (int)f; //此處對f使用了強制類型轉換(int)f,轉換后的值為78 System.out.println("res: "+res); //res: 200 } }
三、運算符與表達式
1. 算術運算符
public class Test003 { public static void main(String[] args) { int i = 5; System.out.println(0/i); //0 System.out.println(0%i); //0 System.out.println(i/0); //除數不能為零,報異常java.lang.ArithmeticException System.out.println(i%0); //除數不能為零,報異常java.lang.ArithmeticException } }
2. 賦值運算符
3. 自增自減運算符(++,--)
public class Test003 { public static void main(String[] args) { int i = 5; System.out.println(i++); //5 System.out.println(++i); //7 System.out.println(i); //7 System.out.println(--i); //6 System.out.println(i--); //6 System.out.println(i); //5 } }
4. 關系運算符
5. 邏輯運算符
public class Test003 { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println(t && f); //false,短路與運算符,若運算符左側為false則不計算右側的表達式 System.out.println(t || f); //true,短路或運算符,若運算符左側為true則不計算右側的表達式 System.out.println(t & f); //false,與運算符,不管左側是否為false都要計算右側的表達式 System.out.println(t | f); //true,或運算符,不管左側是否為true都要計算右側的表達式 System.out.println(t ^ f); //true,異或運算符,只要左右兩側不相同則為true,反之為false System.out.println(!f); //true,取反運算符 } }
6. 位運算符
public class Test003 { public static void main(String[] args) { //在位運算符中1相當於true,0相當於false int b1 = 6; //二進制為00000000 00000000 00000000 00000110 int b2 = 11; //二進制為00000000 00000000 00000000 00001011 System.out.println(b1 & b2); //按位與運算符,二進制為00000000 00000000 00000000 00000010,結果為2 System.out.println(b1 | b2); //按位或運算符,二進制為00000000 00000000 00000000 00001111,結果為15 System.out.println(b1 ^ b2); //按位異或運算符,二進制為00000000 00000000 00000000 00001101,結果為13 System.out.println(~b1); //按位取反運算符,二進制為11111111 11111111 11111111 11111001,結果為-7 System.out.println(b1 << 2); //左移位運算符,二進制為00000000 00000000 00000000 00011000,結果為24 int b3 = -14; //二進制為11111111 11111111 11111111 11110010 System.out.println(b3 >> 2); //帶符號右移位運算符,二進制為11111111 11111111 11111111 11111100,結果為-4 System.out.println(b3 >>> 2); //無符號右移位運算符,二進制為00111111 11111111 11111111 11111100,結果為1073741820 } }
7. 三元運算符
public class Test003 { public static void main(String[] args) { int a = 1; int b = 2; int c = 4; int res = c==a+b?++a:c>a+b?++b:++c; //三元運算符 (表達式)?(值1):(值2),若表達式為true則取值1,反之取值2 System.out.println(res); //++b,結果為3 } }
8. 運算符優先級
四、數組
1. 一維數組
public class Test003 { public static void main(String[] args) { int[] i; //聲明一個整型的一維數組變量 int ii[]; //聲明一個整型的一維數組變量 i = new int[5]; //創建一個長度為5的一維數組對象,並將變量i指向該對象 float[] f = new float[5]; //直接創建一個長度為5的單精度浮點型一維數組對象,並將變量f指向該對象 double[] d = {1, 2, 3.4, 4.5}; //直接初始化一個一維數組元素 System.out.println(d[3]); //通過數組下標來獲取數組內的元素,數組下標從0開始,結果為4.5 System.out.println(f[0]); //當創建出一個數組對象時,該對象內的數組元素為該數據類型的默認值,所以此處結果為0.0 //System.out.println(i[5]); //當通過數組下標來獲取數組內元素時,[]內的值>=數組長度則報異常java.lang.ArrayIndexOutOfBoundsException(數組下標越界) //System.out.println(ii[0]); //若一個數組變量只聲明而未指向某一個具體的數組對象時,編譯出錯 System.out.println(d.length); //得到該數組的長度,結果為4 } }
2. 二維數組
public class Test003 { public static void main(String[] args) { int[][] i; //聲明一個整型的二維數組變量 int ii[][]; //聲明一個整型的二維數組變量 int[] iii[]; //聲明一個整型的二維數組變量 i = new int[5][2]; //創建一個長度為5的二維數組對象,並將變量i指向該對象 float[][] f = new float[5][2]; //直接創建一個長度為5的單精度浮點型二維數組對象,並將變量f指向該對象 double[][] d = {{1}, {2,3}, {4,5,6}, {7,8,9,10}}; //直接初始化一個二維數組元素 System.out.println(d[3][1]); //通過數組下標來獲取數組內的元素,數組下標從0開始,結果為8 System.out.println(f[0][0]); //當創建出二個數組對象時,該對象內的數組元素為該數據類型的默認值,所以此處結果為0.0 //System.out.println(i[5][0]); //當通過數組下標來獲取數組內元素時,[]內的值>=數組長度則報異常java.lang.ArrayIndexOutOfBoundsException(數組下標越界) //System.out.println(ii[0][0]); //若一個數組變量只聲明而未指向某一個具體的數組對象時,編譯出錯 System.out.println(d.length); //得到該數組的長度,結果為4 System.out.println(d[2].length); //得到二位數組內的下標為2的那個一維數組的長度 } }
五、流程控制語句(if,switch,for,while,do...while)
1. 條件分支語句
public class Test003 { public static void main(String[] args) { int[] score = new int[5]; score[0] = -7; score[1] = 65; score[2] = 80; score[3] = 90; score[4] = 59; for(int i=0; i<score.length; i++) { if(score[i]>=0 && score[i]<60) { System.out.println("不及格"); }else if(score[i]>=60 && score[i]<80) { System.out.println("及格"); }else if(score[i]>=80 && score[i]<90) { System.out.println("良"); }else if(score[i]>=90 && score[i]<100) { System.out.println("優"); }else { System.out.println("成績異常"); } } char ch = 'a'; switch(ch) { //switch括號內只支持 byte,short,int,char,enum五種數據類型,但是JDK1.7版本增加了String類型,所以相對於JDK1.7而言就是六種了 case 'A': //case為switch語句的入口,break為出口,從入口開始執行,直到遇到出口或代碼執行完畢才結束 case 'a': System.out.println("優"); break; case 'B': case 'b': System.out.println("良"); break; case 'C': case 'c': System.out.println("及格"); break; default: //若上述條件均不匹配,則進default開始執行語句 System.out.println("不及格"); } } }
2. 循環語句
public class Test003 { public static void main(String[] args) { int res = 0; out: //out是一個標號,告訴java從哪里開始執行程序 for(int i=1; i<=10; i++) { if(i==3) continue out; //continue終止本次循環,執行下次循環 if(i==5) break out; //break跳出循環 res = res + i; } System.out.println(res); //結果為1+2+4=7 int res2 = 0; int i = 1; in: do{ if(i==3) continue in; //continue終止本次循環,執行下次循環 if(i==5) break in; //break跳出循環 res2 = res2 + i; i++; }while(i<=10); System.out.println(res2); } }