一、   Java中的运算符

   Java中,按功能划分主要包含一下运算符。

   1算术运算符

2.关系运算符

3.布尔运算符

4.赋值运算符

5.字符串连接运算符

6.条件运算符

7.其他运算符,如:instanceofnew

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       基本赋值运算符与扩展赋值运算符之间的区别

   通过下面的例子说明基本赋值运算符和扩展赋值运算符之间的区别,主要涉及的是byteshortchar类型变量的赋值运算,使用扩展赋值运算符可能会损失精度。

public class OperatorTest06 {

 

       public  static void main(String[] args){

              //  字面值没有超出byte类型的取值范围,直接赋值

              byte  b = 10;

              //  编译无法通过,因为byte类型的变量先转换成int类型在进行计算

       // 所以运算结果是int类型,而变量bbyte类型,需要进行强制类型转换

              //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必须是布尔表达式,结果只能是truefalse,如果表达式1true则输出表达式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);

              }

       }

}

 

   需要注意,byteshortchar类型可以作为变量的类型,因为这些类型会自动转换成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的值的类型可以是byteshortintchar类型。不能是booleanlongdoublefloat类型。

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);

       }

}

 

需求一:1100奇数的和

需求二:110偶数的乘积

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语句的级别要高于breakcontinue,当遇到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("这条语句不会执行到!");

       }

}