3. 运算符,表达式和语句
3.1 运算符与表达式
Java提供了丰富的运算符,如算数运算符,关系运算符,逻辑运算符,位运算符等。
3.1.1 算数运算符和算数表达式
- \ %
加减运算符
int a =10;int b =20;int c = a+b;int d = b-a;
乘除,求余运算符
int a =10;int b = a*a;int c = 100%10;
3.1.2 自增减运算符
自增:
++自减:
--这里需要注意的是: ++ — 位置不同会导致不同的结果
x++; // 先读取后自增++x; // 先自增后读取x--; // 先读取后自减--x; // 先自减后读取
示例代码
public class HelloWorld {public static void main(String[] args) {int x, before, after;// 先读取后自增x=10;before = x++;System.out.println("x++ before:" + before + " x:" + x);// 先自自增后读取x = 10;after = ++x;System.out.println("++x after:" + after + " x:" + x);// 先读取后自减x=10;before = x--;System.out.println("x-- before:" + before + " x:" + x);// 先自自减后读取x = 10;after = --x;System.out.println("--x after:" + after + " x:" + x);}}
x++ before:10 x:11++x after:11 x:11x-- before:10 x:9--x after:9 x:9
3.1.3 算数混合运算精度
如果低精度数据和高精度数据参与运算那么结果一定是
高精度数据
public class HelloWorld {public static void main(String[] args) {short x = 2;double PI = 3.14;System.out.println(PI * x);}}
6.28
3.1.4 关系运算符和关系表达式
关系运算符无非就是
- a>b
- a<b
- a==b
- a>=b
- a<=b
- a!=b
public class HelloWorld {public static void main(String[] args) {System.out.println(1 > 2);System.out.println(1 < 2);System.out.println(1 >= 2);System.out.println(1 <= 2);System.out.println(1 == 2);System.out.println(1 != 2);}}
falsetruefalsetruefalsetrue
关系运算符产生结果是 boolean类型,要么关系成立,要么关系不成立
3.1.5 逻辑运算符与逻辑表达式
逻辑运算,有
and(&&) or(||) not(!)
- || 和 && 为二目运算符
- &&: 两边同为 true 结果为true
- ||: 有一个满足 true 结果为true
- ! 为 单目运算符
- 取反 true->false false ->true
public class HelloWorld {public static void main(String[] args) {boolean op1 = true;boolean op2 = false;System.out.println(op1 && op2);System.out.println(op1 || op2);System.out.println(!op1);}}
falsetruefalse
3.1.6 赋值运算符与赋值表达式
赋值运算符是一个耳目运算符,左边的操作元必须是变量,不能是常量或者表达式。
int x =10;// 将 10 赋值 给 x
需要搞清楚
=是复制运算符 而==是比较运算符,二者的寓意是完全不同的
3.1.7 位运算符
整数数据在内存中,以二进制形式表示
7的二进制表示
00000000 00000000 00000000 00000111
最左边是符号位,0表示正数,1表示负数.
按位与运算
同1为1,否则为0
按位或运算
同0位0,否则为1
按位非运算
a为1,b为0;a为0,b为1
按位异或运算
相同为0,不同为1
位异或运算有一个特性:连续两次位异或可得到初始值,那么借助这个特性我们可以写个简单的加密程序.
public class HelloWorld {public static void main(String[] args) {char str[] = new char[]{'晚', '上', '来', '我', '家'};for (int i = 0; i < str.length; i++) {str[i] = (char) (str[i] ^ 'a');}System.out.print("密文:");System.out.println(str);for (int i = 0; i < str.length; i++) {str[i] = (char) (str[i] ^ 'a');}System.out.print("名文:");System.out.println(str);}}
密文:昻乫朄扰寗名文:晚上来我家
上面的 a 就是秘钥,通过位异或运算我们就实现了一个简单的加密程序,快拿去试试吧!
3.1.8 instanceof运算符
这是一个二目运算符,左边操作元是一个对象,右边是一个类。当左边对象是右边类,或者子类创建的对象时,该运算符运算结果为 true,否则为 false
public class HelloWorld {public static void main(String[] args) {Integer a = 10;System.out.println(a instanceof Object);}}
true
3.1.9 运算符综述
运算优先级从高到底
| 描述 | 运算符 |
|---|---|
| 分隔符 | [] () . , ; |
| 对象归类,自增自减,逻辑非 | instanceof ++ — ! |
| 算术乘除运算 | * / % |
| 算术加减运算 | + - |
| 位移与运算 | >> << >>> |
| 大小关系运算 | > >= < <= |
| 相等关系运算 | == != |
| 按位与与运算 | & |
| 按位异或运算 | ^ |
| 按位或 | | |
| 逻辑与运算 | && |
| 逻辑或运算 | || |
| 三目运算符 | ?: |
| 赋值运算符 | = |
3.2 语句概述
方法调用语句
调用方法的语句
System.out.println("Hello,World!");
表达式语句
表达式尾加上分号
int a =10;
复合语句
用 { } 包裹的语句
{int a =10;int b =20;int temp = a;a =b;b=temp;}
空语句
一个分号一个空语句
;
控制语句
用来流程控制的语句
int a=10;if(a%2==0){}else{}
package和import语句
package : 声明包
import: 导入包
3.3 if条件分支语句
3.3.1 if条件分支语句
Java中if语句和别的语言一致,满足条件则执行
if(判断条件){// 满足执行}
3.3.2 if-else 语句
不满足上一个分支条件,则执行else,故称之为
双分支语句
简单来进行奇数偶数判断
public class HelloWorld {public static void main(String[] args) {int a = 10;if (a % 2 !=0) {System.out.println("奇数");} else {System.out.println("偶数");}}}
偶数
如果 if 语句中只有一条语句,那么可以省略
{}
3.3.3 if-else if -else语句
多条件分支语句,根据多个条件来控制程序执行流程
import java.util.Scanner;public class HelloWorld {public static void main(String[] args) {// 某相亲大会Scanner scanner = new Scanner(System.in);System.out.println("有车吗?");boolean hasCar = scanner.nextBoolean();System.out.println("有房吗?");boolean hasHouse = scanner.nextBoolean();System.out.println("月薪多少?");double salary = scanner.nextDouble();if (!hasCar){System.out.println("对不起,不合适");}else if(!hasHouse){System.out.println("你是个好人");}else if (salary<10*1_000){System.out.println("要不还是算了吧");}else{System.out.println("你真棒,我要嫁给你");}}}
有车吗?true有房吗?true月薪多少?1000要不还是算了吧
3.4 switch语句
Switch 是但条件多分支的开关语句。
switch(表达式){case 常量1:break;case 常量2:break;case 常量3:break;case 常量4:break;default:// ....}
public class HelloWorld {public static void main(String[] args) {int num = new Scanner(System.in).nextInt();switch (num){case 1:System.out.println("one");break;case 2:System.out.println("two");break;case 3:System.out.println("three");break;default:System.out.println("啥也不是");}}}
1one2two3three99啥也不是
需要强调的是 switch语句中 表达式的值可以是
byte short int char但是不可以是long类型
3.5 循环语句
循环语句是连续多次执行重复的语句
3.5.1 for循环语句
for(表达式1;表达式2;表达式3){// 循环体}
- 表达式1:循环开始前初始
- 表达式2: 循环判断条件
- 表达式3:单次循环后执行
10以内数之和
public class HelloWorld {public static void main(String[] args) {int sum =0;for (int i=0;i<=10;i++){sum+=i;}System.out.println(sum);}}
55
3.5.2 while循环语句
表达式成立则执行,不成立则不执行
while(表达式){语句}
用while语句改写上面语句
public class HelloWorld {public static void main(String[] args) {int i =0;int sum =0;while (i<=10){sum+=i;i++;}System.out.println(sum);}}
3.5.3 do-while循环语句
do-while 语句和上面语句有些不同,
他至少要无条件执行一次循环体
public class HelloWorld {public static void main(String[] args) {int sum = 0, i = 0;do {sum += i;i++;} while (i <= 10);System.out.println(sum);}}
3.6 break和continue语句
break用来终止循环,continue用来跳出循环
输出10以内的所有奇数
public class HelloWorld {public static void main(String[] args) {for (int i=0;i<10;i++){if (i%2==0){continue;}System.out.println(i);}}}
13579
break 来跳出循环
public class HelloWorld {public static void main(String[] args) {for (int i=0;i<10;i++){if (i%2==0){continue;}if(i>=6){break;}System.out.println(i);}}}
135
3.7 for 语句与数组
使用 范围for 来遍历数组
for(ele:数组名){}
使用 范围 for遍历数组
public class HelloWorld {public static void main(String[] args) {int[] ints = {1, 3, 4, 5, 6, 7, 9};for (int ele : ints) {System.out.println(ele);}}}
1345679
传统方式遍历数组
public class HelloWorld {public static void main(String[] args) {int[] ints = {1, 3, 4, 5, 6, 7, 9};for (int i = 0; i < ints.length; i++) {System.out.println(ints[i]);}}}
3.8 小结
- Java提供了丰富的运算符,算数运算符,关系运算符,逻辑运算符,位运算符等
- Java的控制语句和C很类似
- Java提供了遍历数组的循环语句
