Java运算符
Java语言支持如下运算符
- 算数运算符: + , - , * , / , %(取余), ++ , –
- 赋值运算符:=
- 关系运算符: > , < , >= , <= , == (Java中的等于用两个等号), !=(不等于) , instanceof
- 逻辑运算符: && , || , ! 与或非
- 位运算符: & , | , ^ , ~ , >> , << , >>>
- 条件运算符: ?:
- 扩展赋值运算符:+= , -= , *= , /=
四则运算
package com.link.operator;
public class Demo01 {
public static void main(String[] args) {
int a=10;
int b=20;
int c=25;
int d=25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/0041c1a60ff545b6a4a932d8ee77987d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p6X5Lit5bCP5bGLMDgwOA==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
运算时的数据类型转换
package com.link.operator;
public class Demo02 {
public static void main(String[] args) {
long a=123123123123L;
int b=123;
short c=10;
byte d=8;
System.out.println(a+b+c+d);
System.out.println(b+c+d);
System.out.println(c+d);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/1f65fd2010e543beac8368ff1795e17b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p6X5Lit5bCP5bGLMDgwOA==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
关系运算符
package com.link.operator;
public class Demo03 {
public static void main(String[] args) {
int a=10;
int b=20;
int c=11;
System.out.println(c%a);
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/4a7ecfd25cec43fd92a6e5f870f8c6a7.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p6X5Lit5bCP5bGLMDgwOA==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
自增自减
package com.link.operator;
public class Demo04 {
public static void main(String[] args) {
int a=3;
int b=a++;
System.out.println(a);
int c=++a;
System.out.println(a);
System.out.println(b);
System.out.println(c);
double pow = Math.pow(3, 2);
System.out.println(pow);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/456b0b0677944acf9b43cfae1ba315af.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p6X5Lit5bCP5bGLMDgwOA==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
逻辑运算符
package com.link.operator;
public class Demo05 {
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);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/86d5ba8814614541af2ac51998ac0ad9.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p6X5Lit5bCP5bGLMDgwOA==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
位运算
package com.link.operator;
public class Demo06 {
public static void main(String[] args) {
System.out.println(2<<3);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/ceb67542ca824542ad594c139b6ce0d8.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p6X5Lit5bCP5bGLMDgwOA==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
扩展赋值运算符
package com.link.operator;
public class Demo07 {
public static void main(String[] args) {
int a1=10;
int a2=10;
int b=20;
a1+=b;
a2-=b;
System.out.println(a1);
System.out.println(a2);
System.out.println(a1+b);
System.out.println(""+a1+b);
System.out.println(a1+b+"");
System.out.println(a1+""+b);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/bf851008ce594ed4bf3651da75f4a7ec.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p6X5Lit5bCP5bGLMDgwOA==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
条件运算符
package com.link.operator;
public class Demo08 {
public static void main(String[] args){
int score=78;
String type= score<60?"不及格":"及格";
System.out.println(type);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/6f8468bd2bc6416daecc6b549bfa1ba0.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5p6X5Lit5bCP5bGLMDgwOA==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)