算数运算符
算数运算符
1.算术运算符
| 运算符 | 描述 | 示例 | 结果 |
|---|---|---|---|
| + | 加 | 1+1 | 2 |
| - | 减 | 1-1 | 0 |
| * | 乘 | 1*1 | 1 |
| / | 除 | 5/2 | 2 |
| % | 取模(取余) | 5%2 | 1 |
| ++ | 自增 | 1++,++1 | 1,2 |
| -- | 自减 | 1--,--1 | 1,0 |
public class Demo1 {
public static void main(String[] args) {
//a++ 先赋值再自增
int a = 0;
System.out.println("a++ = "+ a++);
System.out.println("a++后,a = "+ a);
//b-- 先自增再赋值
int b = 0;
System.out.println("++b = "+ ++b);
System.out.println("++b后,b = "+b);
}
}
运行截图如下:
2.赋值运算符
| 运算符 | 描述 | 示例 | 结果 |
|---|---|---|---|
| = | 赋值 | a = 3,b = 5 | a = 3,b = 5 |
| += | 加等于 | a = 2,b = 5,a += b | a = 7,b = 5 |
| -= | 减等于 | a = 2,b = 5,a -= b | a = -3,b = 5 |
| *= | 乘等于 | a = 2,b = 5,a *= b | a = 10,b = 5 |
| /= | 除等于 | a = 2,b = 5,a /= b | a = 0,b = 5 |
| %= | 模等于 | a = 2,b = 5,a %= b | a = 2,b = 5 |
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 2;
int b = 5;
System.out.println("a += b,a = "+( a += b )+",b = "+b);
System.out.println("a -= b,a = "+( a -= b )+",b = "+b);
System.out.println("a *= b,a = "+( a *= b )+",b = "+b);
System.out.println("a /= b,a = "+( a /= b )+",b = "+b);
System.out.println("a %= b,a = "+( a %= b )+",b = "+b);
}
}
运行截图:
3.关系运算符
关系运算符作用是比较两边的操作数,结果总是boolean型。
| 运算符 | 描述 | 示例 | 结果 |
|---|---|---|---|
| == | 相等于 | 4 == 3 | false |
| != | 不等于 | 4 != 3 | true |
| < | 小于 | 4 < 3 | false |
| > | 大于 | 4 > 3 | true |
| <= | 小于等于 | 4 <= 3 | false |
| >= | 大于等于 | 4 >= 3 | true |
public class Demo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("4 == 3,结果:"+( 4 == 3));
System.out.println("4 != 3,结果:"+( 4 != 3));
System.out.println("4 < 3,结果:"+( 4 < 3));
System.out.println("4 > 3,结果:"+( 4 > 3));
System.out.println("4 <= 3,结果:"+( 4 <= 3));
System.out.println("4 >= 3,结果:"+( 4 >= 3));
}
}
运行截图:
4.逻辑运算符
逻辑运算符用于对boolean类型结果的表达式进行运算,运算结果总是boolean型。
| 运算符 | 描述 | 示例 | 结果 |
|---|---|---|---|
| & | 与 | false & true | false |
| | | 或 | false | true | true |
| ^ | 异或 | false ^ true | true |
| ! | 非 | !true | false |
| && | 短路与 | false && true | false |
| || | 短路或 | false || true | true |
在计算机中默认1为真,实际上只要非零即真,0为假。
public class Demo4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("false & true,结果:"+(false & true));
System.out.println("false | true,结果:"+(false | true));
//异或:左右相同是false,左右不同是true
System.out.println("false ^ true,结果:"+(false ^ true));
System.out.println("! true,结果:"+!true);
//短路与:左侧是false 就不执行右侧
System.out.println("false && true,结果:"+(false && true));
//短路或:结果与"与"一样,左边是true就不执行右边
System.out.println("false || true,结果:"+(false || true));
}
}
运行截图:
5.字符串连接符
public class Demo5 {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "world!";
System.out.println(str1);
System.out.println(str2);
System.out.println(str1+" "+str2);
//+左右任何一侧是字符串,则+的含义会从算术运算符的"+"转换成字符串连接符
System.out.println(str1+" "+1);
}
}
运行截图:
6.三目运算符
表达式 ? value1 : value2
public class Demo6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//boolean类型结果的表达式 ? 值1 : 值2
//boolean表达式若为true则整个三目运算符运算结果为值1,否则为值2
boolean a = true;
boolean b = false;
System.out.println(a?"a = true":"a = false");
System.out.println(b?"b = true":"b = false");
}
}
运行结果:

浙公网安备 33010602011771号