一、 Java中的運算符
在Java中,按功能劃分主要包含一下運算符。
1.算術運算符
2.關係運算符
3.布爾運算符
4.賦值運算符
5.字符串連接運算符
6.條件運算符
7.其他運算符,如:instanceof,new
1.1 算術運算符
算術運算符:
算術運算符 | 描述 |
+ | 相加 |
- | 相減 |
* | 相乘 |
/ | 返回整除的值 |
% | 返回餘數 |
++ | 自加一,如果++出現在變量的前面,先自加一在進行其他運算 如果++出現在變量的後面,先進行其他運算在自加一 |
-- | 自減一,原理同上 |
通過一個例子說明。
public class OperatorTest01 {
public static void main(String[] args){ // 聲明兩個整型變量 int i = 11; int j = 4; // 進行算術運算 System.out.println(i + j); System.out.println(i - j); System.out.println(i * j); System.out.println(i / j); System.out.println(i % j); // ++運算符在變量的前面或後面變量都會自加一 int k = 10; k++; System.out.println(k); // 11 ++k; System.out.println(k); // 12
k = 10; int l = k++; System.out.println(l); // 10 System.out.println(k); // 11
k = 10; l = ++k; System.out.println(l); // 11 System.out.println(k); // 11
// ++運算符在變量的後面,先進行其他運算然後自加一 k = 10; System.out.println(k++); // 10 System.out.println(k); // 11 // ++運算符在變量的前面,先自加一在進行其他運算 k = 10; System.out.println(++k); // 11 } } |
1.2 關係運算符
關係運算符:
關係運算符 | 描述 |
< | 小於 |
<= | 小於等於 |
> | 大於 |
>= | 大於等於 |
== | 等於 |
!= | 不等於 |
關係運算的結果一定是一個布爾類型,如:10>3是真。
public class OperatorTest02 {
public static void main(String[] args){
int i = 10; int j = 5;
System.out.println(i > j); System.out.println(i >= j); System.out.println(i < j); System.out.println(i <= j); System.out.println(i == j); System.out.println(i != j);
boolean b = i < j; } } |
1.3 布爾運算符
布爾運算符:
布爾運算符 | 描述 |
&& | 短路與 |
|| | 短路或 |
& | 與 |
| | 或 |
! | 非(取反) |
^ | 異或 |
布爾運算符兩邊的算子必須都是布爾類型,整個表達式的運算結果也是一個布爾類型。
布爾運算:
A | B | A && B | A || B | A & B | A | B | A ^ B | !A |
true | true | true | true | true | true | false | false |
false | false | false | false | false | false | false | true |
true | false | false | true | false | true | true | false |
false | true | false | true | false | true | true | true |
public class OperatorTest03 {
public static void main(String[] args){
System.out.println(10>3 && 10<20); System.out.println(10>3 & 10<20); System.out.println(10>3 || 10<20); System.out.println(10>3 | 10<20); System.out.println(10>3 ^ 10<20); System.out.println(!(10>3));
System.out.println(true && false); System.out.println(false & false); System.out.println(false || true); System.out.println(false | true); System.out.println(false ^ true); System.out.println(!false);
boolean b = 10>3 && 10<20; System.out.println(b); } } |
1.3.1 &&與&的區別
短路與&&,首先判斷第一個操作數,如果值爲false則不再判斷第二個操作數,因爲兩個操作數只要有一個爲假,則結果爲假。如果第一個操作數爲true,在判斷第二個操作數的結果。而邏輯與&,不管第一個操作數是否爲false,都會計算第二個操作數。
public class OperatorTest04 {
public static void main(String[] args){ // 邏輯與不管第一個操作數是否爲false,都會計算第二個操作數 int i = 10; int j = 20; boolean b = i > j & i++ <= j; System.out.println(b); System.out.println(i); // 短路與先判斷第一個操作數是否爲false,如果第一個操作數結果爲false // 則不會在計算第二個操作數,所以效率較高 i = 10; j = 20; b = i > j && i++ <= j; System.out.println(b); System.out.println(i); } } |
1.3.2 ||與|的區別
邏輯或與短路或的區別和短路與與邏輯與相似,邏輯或不管第一個操作數的結果是否爲true都會計算第二個操作數,而短路或只要第一個操作數的結果爲true,則不會在計算第二個操作數。所以短路或和短路與一樣,效率較高。所以使用率較高並且建議使用。
1.4 賦值運算符
賦值運算符:
賦值運算符 | 描述 |
= | 將值賦給變量 |
+= | a += 10; 等同於 a = a + 10; |
-= | a -= 10; 等同於 a = a - 10; |
*= | a *= 10; 等同於 a = a * 10; |
/= | a /= 10; 等同於 a = a / 10; |
%= | a %= 10; 等同於 a = a % 10; |
public class OperatorTest05 {
public static void main(String[] args){
int i = 10;
i += 10; System.out.println(i);
i -= 10; System.out.println(i);
i *= 10; System.out.println(i);
i /= 10; System.out.println(i);
i %= 10; System.out.println(i); } } |
1.4.1 基本賦值運算符與擴展賦值運算符之間的區別
通過下面的例子說明基本賦值運算符和擴展賦值運算符之間的區別,主要涉及的是byte、short和char類型變量的賦值運算,使用擴展賦值運算符可能會損失精度。
public class OperatorTest06 {
public static void main(String[] args){ // 字面值沒有超出byte類型的取值範圍,直接賦值 byte b = 10; // 編譯無法通過,因爲byte類型的變量先轉換成int類型在進行計算 // 所以運算結果是int類型,而變量b是byte類型,需要進行強制類型轉換 //b = b + 20; //編譯通過,說明擴展賦值運算符沒有將byte類型變量b轉換成int類型 // 並沒有改變運算結果類型,結果還是byte類型,但是需要注意的是 // 可能會損失精度 b += 20; // 編譯通過,但是精度損失嚴重,因爲byte類型的上限值是128 b += 5000; } } |
1.5 字符串連接運算符
“+”符既可以進行算術運算有可以做字符串連接符,那麼在什麼情況進行算術運算!在什麼情況下進行字符串連接!
1.做加法運算(+號兩邊只要都是數值類型,一定是加法運算)
2.字符串連接(+號兩邊任意一端只要是字符串類型,則一定是字符串連接)
public class OperatorTest07 {
public static void main(String[] args){
String s = "PI = " + 3.14; System.out.println(s); // PI = 3.14
int i = 10; int j = 20; System.out.println("i + j = " + i + j); // i + j = 1020
System.out.println("i + j = " + (i + j)); // i + j = 30
System.out.println(i + " + " + j + " = " + (i + j)); // 10 + 20 = 30 } } |
1.6 條件運算符
條件運算符是java語言中的三元運算,格式如下:
表達式1 ? 表達式2 : 表達式3 |
表達式1必須是布爾表達式,結果只能是true或false,如果表達式1爲true則輸出表達式2的值,否則輸出表達式3的值。
通常情況下,表達式2和表達式3的類型相同,可以將調解運算符運算結果賦值給一個變量,注意:變量的類型要和表達式2和表達式3的類型相同。
public class OperatorTest08 {
public static void main(String[] args){
int i = 10 > 5 ? 0 : 1; System.out.println(i);
boolean b = 10 % 4 == 0 ? true : false; System.out.println(b); } } |
二、 Java控制語句
在Java中控制語句可以分爲以下三類。
1.控制選擇結構語句或分支語句
if 語句
if else語句
switch(int)
2.控制循環結構語句
for循環語句
while循環語句
do while循環語句
3.改變語句執行順序
break語句
continue語句
return語句
下面對不同類別的控制語句分別進行說明。
2.1 選擇結構語句或分支語句
2.1.1 if語句
單一的if語句只有一個執行分支,語法結構如下。
if(布爾表達式){ 語句塊; } |
當布爾表達式爲true時,纔會執行if語句內的語句塊,當語句塊內的語句執行結束後,接着執行if語句後面的語句,順序向後執行。如果布爾表達式爲false,程序執行流程會跳過if語句繼續執行後面的語句。
如果if語句中的語句塊中只有一條語句,可以省略大括號。
if(布爾表達式) 語句; |
但是爲了程序的可讀性更好,建議使用大括號將語句括起來。
public class IfElseTest01 {
public static void main(String[] args){
int a = 10; int b = 5; if(a > b){ System.out.println("變量a大於變量b"); }
if(a % b == 0){ System.out.println("變量a可以被變量b整除"); }
if(a - b > 0) System.out.println("變量a大於變量b"); // System.out.println("如果多於一條語句,要加大括號"); if(true) System.out.println("這條語句一定會執行"); } } |
2.1.2 if…else語句
語法結構有下面幾種情況。
第一種情況 if(布爾表達式){ 語句塊; }else{ 語句塊; } 當if布爾表達式爲true時,執行if後面的語句塊,表達式爲false時,執行else後面的語句塊。
第二種情況 if(布爾表達式){ 語句塊; }else if(布爾表達式){ 語句塊; }else if(布爾表達式){ 語句塊; } 點if表達式爲true時,執行if後面的語句塊。當表達式爲false時,會接着判斷else後面的if中的布爾表達式的值是否爲true,如果爲true,那麼執行if後面的語句塊,否則繼續判斷下面else後面的if布爾表達式的值是否爲true,如果爲true,則執行if後面的語句塊,執行結束後繼續執行if分支語句後面的語句。如果爲false,則直接執行if分支語句後面的語句。
第三種情況 if(布爾表達式){ 語句塊; }else if(布爾表達式){ 語句塊; }else if(布爾表達式){ 語句塊; }else{ 語句塊; } 第三種情況和第二種情況和類似,不同的是在最後還有一個else語句塊。判斷從上向下,先判斷布爾表達式的值,根據布爾表達式的值決定執行不同的分支語句。 |
需要注意的是,if語句中只要有一個分支語句塊被執行,則整個if語句結束,並執行後面的語句。上面的三種結構中第一種和第三種方式中可以保證一定會有一個分支語句被執行,因爲在語句的最後都有else語句。
public class IfElseTest02 {
public static void main(String[] args){
int a = 10; int b = 5; if(a > b){ System.out.println("變量a大於變量b"); }else{ System.out.println("變量a小於變量b"); }
a = 15; b = 4;
if(a % b == 0){ System.out.println("變量a可以被變量b整除"); }else{ System.out.println("變量a不可以被變量b整除"); }
boolean bo = false;
if(bo){ System.out.println("布爾表達式的值爲true"); }else{ System.out.println("布爾表達式的值爲false"); } } } |
多重分支的例子。
public class IfElseTest03 {
public static void main(String[] args){
int score = 90;
if(score >= 0 && score < 60){ System.out.println("不及格"); }else if(score == 60){ System.out.println("及格"); }else if(score > 60 && score < 80){ System.out.println("中"); }else if(score >= 80 && score < 90){ System.out.println("良"); }else{ System.out.println("優"); }
String info = "優";
if(score >= 0 && score < 60){ info = "不及格"; }else if(score == 60){ info = "及格"; }else if(score > 60 && score < 80){ info = "中"; }else if(score >= 80 && score < 90){ info = "良"; }
System.out.println(info); } } |
2.1.3 switch語句
switch分支語句的語法格式如下。
switch(int類型的變量){ case int類型的值: 語句塊; break; case int類型的值: 語句塊; break; case int類型的值: 語句塊; case int類型的值: 語句塊; break; default: 語句塊; } |
當程序執行到switch語句是,首先要判斷switch後面是否是int類型的變量,如果不是,編譯不能通過。否則,從上向下判斷int類型的值和哪個case後面的值匹配,如果找到匹配的case,則執行case下面的語句塊,如果沒有找到則執行default後面的語句塊。需要注意的是,case語句塊中如果存在break,當執行到break時,程序的執行流程會跳出switch執行後面的語句,如果case中沒有break,則會發生case穿透現象,繼續執行case分支後面的case語句,直到遇到break,纔會跳出switch語句。另外,break語句和default默認分支語句可以沒有,下面通過例子說明。
public class SwitchTest01 {
public static void main(String[] args){ // 編譯不能通過,switch接受的類型爲int類型 // long score = 90; int score = 90;
switch(score){ case 100: System.out.println(12); } } } |
需要注意,byte、short和char類型可以作爲變量的類型,因爲這些類型會自動轉換成int類型,如下面的例子。
public class SwitchTest02 {
public static void main(String[] args){
char c = 'A';
switch(c){ case 'A': System.out.println("優"); break; case 'B': System.out.println("良"); break; case 'C': System.out.println("中"); break; case 'D': System.out.println("及格"); break; default: System.out.println("不及格"); } } } |
switch的值的類型可以是byte、short、int和char類型。不能是boolean、long、double和float類型。
public class SwitchTest03 {
public static void main(String[] args){
//byte score = 80; //short score = 90; int score = 70;
switch(score){ case 90: System.out.println("優"); break; case 80: System.out.println("良"); break; case 70: System.out.println("中"); } } } |
如果case語句中沒有break語句,將會發生case穿透現象,繼續執行下面的case語句,直到遇到break。
public class SwitchTest04 {
public static void main(String[] args){
int score = 90;
switch(score){ case 90: System.out.println("優"); // 因爲沒有break語句,所以會繼續向下執行 case 80: System.out.println("良"); // 會執行,直到遇到break語句,跳出switch break; case 70: System.out.println("中"); } } } |
case語句可以合併,將多個case攔截執行相同的語句塊,如下例子。
public class SwitchTest05 {
public static void main(String[] args){
// int score = 90; int score = 80;
switch(score){
case 90:case 80: System.out.println("優"); break; case 70:case 60: System.out.println("良"); break; default: System.out.println("中"); } } } |
case合併的典型例子。
public class SwitchTest06 {
public static void main(String[] args){
//int score = 50; //int grade = score/10;
double score = 78.4; int grade = (int)score/10; // 將double類型的值強行轉換成int類型在被10除
switch(grade){
case 10:case 9: System.out.println("優"); break; case 8:case 7: System.out.println("良"); break; case 6: System.out.println("中"); break; default: System.out.println("及格"); } } } |
2.2 控制循環結構語句
2.2.1 for循環
for循環的語法格式如下。
for(表達式1;表達式2;表達式3) { 語句塊; } |
在for循環中,表達式1是初始化表達式,最先執行,只執行一次。表達式2必須是布爾類型的表達式。會執行多次。表達式3是條件操作語句,會執行多次。
for循環開始執行時,先執行表達式1,並且只執行一次。然後判斷表達式2的結果,如果是true,則執行java語句塊,當語句塊執行完成後,再執行表達式3,對控制變量進行修改,然後再次判斷表達式2的結果,執行語句塊內容,直到表達式2的結果是false時,纔會結束for循環,執行for循環後面的語句。
在for循環中,三個表達式都不是必須存在的,如下例子。
public class ForTest01 {
public static void main(String[] args){ // 無限循環 for(;;){ System.out.println("無限循環..."); } } } |
for循環中的變量聲明方式可以採用下面的幾種方式,但是要注意變量的作用域。
public class ForTest02 {
public static void main(String[] args){
for(int i = 0;i < 10;i++){ System.out.println("i = " + i); }
int j = 0; for(;j < 10;j++){ System.out.println("j = " + j); }
int k; for(k = 0;k < 10;k++){ System.out.println("k = " + k); }
for(int l = 10;l > 0;l--){ System.out.println("l = " + l); }
for(int m = 0;m < 10;m+=2){ System.out.println("m = " + m); } } } |
for循環可以進行嵌套,在for循環中存在for循環,可以多層嵌套。
public class ForTest03 {
public static void main(String[] args){
for(int i = 10;i >= 0;i--){ for(int j = 0;j < i;j++){ System.out.print(" " + j); }
System.out.println(); } } } |
輸出結果:
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 0 1 2 3 4 5 0 1 2 3 4 0 1 2 3 0 1 2 0 1 0 |
for循環的典型例子,九九乘法表。
public class ForTest04 {
public static void main(String[] args){
for(int i = 1;i <= 9;i++){ for(int j = 1;j <= i;j++){ System.out.print(j + " * " + i + " = " + (i * j) + " "); }
System.out.println(); } } } |
控制檯輸出:
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 |
for循環常見的面試題,求1~100的偶數的和。
public class ForTest05 {
public static void main(String[] args){
int sum = 0;
for(int i = 1;i <= 100;i++){ if(i % 2 == 0){ sum += i; } }
System.out.println("1 to 100 of the sum: " + sum);
sum = 0;
for(int i = 2;i <= 100;i+=2){ sum += i; }
System.out.println("1 to 100 of the sum: " + sum); } } |
需求一:1到100奇數的和
需求二:1到10偶數的乘積
2.2.2 while循環
while循環的語法結構如下。
while(布爾表達式){ 語句塊; } |
當布爾表達式爲true時,執行循環體內的語句塊。只有當表達式的值爲false時,退出循
環。while循環與for循環的不同之處在於表達式,for循環的循環次數事先可以基本確
定,並且在表達式3中不斷的改變循環條件,而while循環需要在循環體中改變循環條
件,否則會無限循環下去。另外,while循環的次數是0~多次,如下面的例子。
public class WhileTest01 {
public static void main(String[] args){
while(true){ System.out.println("無限循環..."); } } } |
while循環需要在循環體中改變循環條件。
public class WhileTest02 {
public static void main(String[] args){
int i = 0; /* while(i < 10){ System.out.println("i = " + i); // 無限循環 } */ while(i < 10){ i++; System.out.println("i = " + i); }
i = 0; while(i < 10){ System.out.println("i = " + i); i++; } } } |
2.2.3 do…while循環
do…while循環的語法如下。
do{ 循環體; }while(布爾表達式); |
首先執行一次循環體,然後在進行條件判斷,所以do…while循環至少執行一次循環體。同樣,循環條件的修改在循環體中,如果不改變循環條件,會發生無限循環。
public class DoWhileTest01 {
public static void main(String[] args){
int i = 0; /* do{ System.out.println("i = " + i); // 無限循環 }while(i < 10); */
do{ System.out.println("i = " + i); i++; }while(i < 10);
i = 0; do{ i++; System.out.println("i = " + i); }while(i < 10); } } |
2.3 改變控制語句順序
2.3.1 break語句
break語句可以用在switch語句中,結束switch分支語句。如果break語句使用在循環語句中,會結束循環,執行循環語句後面的語句。如果是嵌套循環,那麼會終止離他最近的循環。
public class BreakTest01 {
public static void main(String[] args){
for(int i = 0;i < 10;i++){ if(i == 8){ break; } System.out.println("i = " + i); } } } |
break語句只結束離他最近的循環,當前循環的外層循環還會繼續循環,知道循環條件不成立。另外需要注意的是,break語句的後面不能跟着其他的語句,因爲不會執行到,編譯不能通過。
public class BreakTest02 {
public static void main(String[] args){
for(int i = 0;i < 10;i++){ for(int j = 0;j < 10;j++){ if(j == 5){ break; //System.out.println("j = " + j); }
System.out.println("i = " + i + " j = " + j); } } } } |
while循環的循環條件需要在循環體內進行修改,如下例子。
public class BreakTest03 {
public static void main(String[] args){
int i = 0; while(i < 10){ for(;i < 20;i++){ if(i == 10){ break; }
System.out.println("i = " + i); } } } } |
break語句還可以指定終止的循環,但是需要需要爲終止的循環指定別名,在break語句中要指定要終止的循環的別名。
public class BreakTest04 {
public static void main(String[] args){
f: for(int i = 0;i < 10;i++){ for(int j = 0;j < 10;j++){ if(j == 5){ break f; } System.out.println("i = " + i + " j = " + j); } } } } |
又一個例子。
public class BreakTest05 {
public static void main(String[] args){
f: while(true){ for(int i = 0;i < 10;i++){ if(i == 5){ break f; } System.out.println("i = " + i); } } } } |
2.3.2 continue語句
continue語句使用在循環中,但是continue不會終止整個循環,只是終止本次循環,開始執行下一次循環,continue語句後面的語句不會在執行。
public class ContinueTest01 {
public static void main(String[] args){
for(int i = 0;i < 10;i++){ if(i == 8){ continue; //System.out.println("i = " + i); // continue後面不能跟語句 } System.out.println("i = " + i); } } } |
如果continue在多重循環中使用,也可以指定要終止的循環,採用的方式如break,爲循環起個別名。
public class ContinueTest02 {
public static void main(String[] args){
f: for(int i = 0;i < 2;i++){ for(int j = 0;j < 10;j++){ if(j == 5){ continue f; }
System.out.println("i = " + i + " j = " + j); } } } } |
2.3.3 return語句
return語句的級別要高於break和continue,當遇到return語句,當前方法會終止繼續執行,返回到當前方法的調用處,繼續向下執行。而break只能使用在循環語句中或switch分支語句中,並且只能終止循環語句或switch語句。
public class ReturnTest01 {
public static void main(String[] args){
for(int i = 0;i < 10;i++){ if(i == 8){ return; //System.out.println("i = " + i); } System.out.println("i = " + i); }
System.out.println("這條語句不會執行到!"); } } |
有一個例子。
public class ReturnTest02 {
public static void main(String[] args){
method(); System.out.println("main: 執行到這裏!"); }
public static void method(){ for(int i = 0;i < 10;i++){ if(i == 5){ return; } System.out.println("i = " + i); }
System.out.println("這條語句不會執行到!"); } } |