运算符
- Java语言支持如下运算符:
- 算术运算符: +, -, *, /, %(取余,模运算), ++, --
- 赋值运算符: =
- 关系运算符: > , <, >= , <=, ==(Java中的等于使用两个符号判断的), !=(不等于), instanceof
- 逻辑运算符: &&(与), ||(或), !(非)
- 位运算符: &, |, ^, ~, >>, <<, >>> (只做了解)
- 条件运算符: ?, :
- 扩展赋值运算符: +=, -=, *=, /=
运算符优先级#
优先级 |
运算符 |
结合性 |
1 |
()、[]、{} |
从左向右 |
2 |
!、+、-、~、++、-- |
从右向左 |
3 |
*、/、% |
从左向右 |
4 |
+、- |
从左向右 |
5 |
«、»、>>> |
从左向右 |
6 |
<、<=、>、>=、instanceof |
从左向右 |
7 |
==、!= |
从左向右 |
8 |
& |
从左向右 |
9 |
^ |
从左向右 |
10 |
| |
从左向右 |
11 |
&& |
从左向右 |
12 |
|| |
从左向右 |
13 |
?: |
从右向左 |
14 |
=、+=、-=、*=、/=、&=、|=、^=、~=、«=、»=、>>>= |
从右向左 |
运算
自增(++)自减(--)运算#
- 自增自减为一元运算, 如a++中仅有一个a. 又如a+1中有a和1, 所以为二元运算.
public class Demo{
public static void main(String[] args){
int a = 3;
int b = a++;
System.out.println(a);
System.out.println(b);
int c = ++a;
System.out.println(a);
System.out.println(c);
}
}
数学运算(Math类)#
public class Demo{
public static void main(String[] args){
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
逻辑运算#
public class Demo{
public static void main(String[] args){
boolean a = true;
boolean b = false;
System.out.println("a && b: "+(a&&b));
System.out.println("a || b: "+(a||b));
System.out.println("!(a && b): "+!(a&&b));
int c = 5;
boolean d =(c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
}
}
位运算#
拓展运算符#
public class Demo{
public static void main(String[] args){
int a = 10;
int b = 20;
a+=b;
a-=b;
System.out.println(a);
System.out.prinln(""+a+b);
System.out.println(a+b+"");
}
}
三元运算符#
public class Demo{
public static void main(String[] args){
int score = 80;
String type = score<60 ?"不及格":"及格";
System.out.println(type);
}
}
视频课程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通