算术运算符一览
| 运算符 | 运算 | 范例 | 结果 |
|---|---|---|---|
| + | 正号 | +7 | 7 |
| - | 负号 | -11 | -11 |
| + | 加 | 9+1 | 10 |
| - | 减 | 9-1 | 8 |
| * | 乘 | 7*8 | 56 |
| 、 | 除 | 9/9 | 1 |
| % | 取模(取余) | 11%8 | 3 |
| ++i | 自增(前):先运算后取值 | a=2;b=++a | b=2 |
| i++ | 自增(后):先取值后运算 | a=2;b=a++ | b=3 |
| –i | 自减(前):先运算后取值 | a=2;b=–a | b=2 |
| i– | 自减(后):先取值后运算 | a=2;b=a– | b=1 |
| + | 字符串相加 | “hello”+“edu” | hello edu |
范例
public class hello{
public static void main(String[] args){
int a = 10;
int b = 3;
String str1 = "hello";
String str2 = "word";
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
System.out.println(a++);
System.out.println(++a);
System.out.println(a--);
System.out.println(--a);
System.out.println(str1+str2);
}
}
运行结果
2、关系运算符关系运算符一览
| 运算符 | 运算 | 范例 | 结果 |
|---|---|---|---|
| == | 相等于 | 8==7 | false |
| != | 不等于 | 8!=7 | true |
| < | 小于 | 9<7 | false |
| > | 大于 | 9>7 | true |
| <= | 小于等于 | 9<=7 | false |
| >= | 大于等于 | 9>=7 | true |
| instanceof | 检查是否是类的对象 | “hello” instanceof String | true |
范例
package com.lizhi;
public class hello{
public static void main(String[] args){
int a = 10;
int b = 3;
String str1 = "hello";
String str2 = "word";
System.out.println(a==b);
System.out.println(a!=b);
System.out.println(a>b);
System.out.println(a=b);
System.out.println(a<=b);
System.out.println(str1 instanceof String);
}
}
*
3、逻辑运算符逻辑运算符一览
| 运算符 | 运算 | 范例 (age = 10) | 结果 |
|---|---|---|---|
| & | 逻辑与 | age >20 & age<100 | false |
| && | 短路与 | age >20 & age<100 | false |
| | | 逻辑或 | age >20 & age<100 | true |
| || | 短路或 | age >20 & age<100 | true |
| ! | 取反 | !(age>20) | true |
| ^ | 逻辑异或 | age >20 & age<100 | true |
范例
package com.lizhi;
public class hello{
public static void main(String[] args){
int age = 10;
if( age>20 & age<90){
System.out.println("ok1");
} else{
System.out.println("no1");
}
if( age>20 && age<90){
System.out.println("ok2");
} else{
System.out.println("no2");
}
if( age>20 | age<90){
System.out.println("ok3");
} else{
System.out.println("no3");
}
if( age>20 || age<90){
System.out.println("ok4");
} else{
System.out.println("no5");
}
if( age>20 ^ age<90){
System.out.println("ok5");
} else{
System.out.println("no5");
}
}
}
运行结果
4、赋值运算符赋值运算符一览
| 运算符 | 运算 | 范例 | 结果 |
|---|---|---|---|
| = | 直接赋值 | i = 11 | |
| += | 先加后赋值 | i += 2 | |
| -= | 先减后赋值 | i -= 2 | |
| *= | 先乘后赋值 | i -= 2 | |
| /= | 先除后赋值 | i /= 2 | |
| %= | 先取余后赋值 | i %= 2 |
范例
package com.lizhi;
public class hello{
public static void main(String[] args){
int i = 11;
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);
i %= 10;
System.out.println("%=:"+i);
}
}
运行结果
5、三元运算符三元运算符:
三元运算符:条件表达式?表达式1:表达式2 (1)如果条件表达式为true,运算后的结果是表达式1; (1)如果条件表达式为false,运算后的结果是表达式2;
范例
package com.lizhi;
public class hello{
public static void main(String[] args){
int a = 11;
int b = 12;
int result = a < b?a--:++b;
System.out.println(result);
System.out.println(a);
System.out.println(b);
}
}
运行结果



