运算符
- Java语言支持如下运算符: 优先级()
- 算术运算符:+, -, *, /, %, ++, –
- 赋值运算符:=
- 关系运算符:>, <, >=, <=, ==, !=instanceof
- 逻辑运算符:&&,||,!
- 位运算符:&,|,^, ~, >>, <<, >>>(了解!!!)
- 条件运算符:?:(三元运算符)
- 扩展赋值运算符:+=,-=,*=,/=
算术运算符
- 二元运算符
package com.example.operator;
/**
* Created by IntelliJ IDEA.
* Date: 2020/9/11
*/
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);// 30
System.out.println(a-b);// -10
System.out.println(a*b);// 200
System.out.println(a/b); //0 注意:定义的是int类型,int类型是整数,所以输出为0。
System.out.println(a/(double)b); //0.5 注意:这里只要加个double强制转换就没问题了。
}
}
-
一元运算符
package com.example.operator; /** * Created by IntelliJ IDEA. * Date: 2020/9/11 */ public class Demo04 { public static void main(String[] args) { // ++ -- 自增,自减 一元运算符 int a = 3; int b = a++; //执行完这行代码后,先给b赋值,再自增 //a++ a = a + 1; System.out.println(a); //4 //a++ a = a + 1; int c = ++a; //执行完这行代码前,先自增,再给b赋值 System.out.println(a); //5 System.out.println(b); //3 System.out.println(c); //5 // -- 和 ++ 反着来的这里就不做演示了 System.out.println("------------分割线------------"); //扩充: 幂运算 2^3(2的3次方) 2*2*2 = 8 很多运算,我们会使用一些工具类来操作! double pow = Math.pow(2, 3); //2^3(2的3次方) 2*2*2 = 8 double pow1 = Math.pow(3, 2); //3^2(3的2次方) 3*3 = 9 System.out.println(pow); //8.0 (因为是double类型所以有.0) System.out.println(pow1); //9.0 (因为是double类型所以有.0) } }
赋值运算符
package com.example.operator;
import java.sql.SQLOutput;
/**
* Created by IntelliJ IDEA.
* Date: 2020/9/11
*/
public class Demo02 {
public static void main(String[] args) {
//赋值运算符
//赋值运算符:用=号将右边的值赋值到左边的变量上就是赋值运算符
long a = 123123123123123L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//123123123123264 Long 自动升级(自动升为混合运算中最高级别)
System.out.println(b+c+d);//141 Int 自动升级(自动升为混合运算中最高级别)
System.out.println(c+d);//18 Int 自动升级(自动升为混合运算中最高级别)
}
}
关系运算符
package com.example.operator;
/**
* Created by IntelliJ IDEA.
* Date: 2020/9/11
*/
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果:正确,错误,布尔值
//if
int a = 10;
int b = 20;
int c = 22;
//取余,模运算
System.out.println(c%a); //2
System.out.println(a>b); //false
System.out.println(a<b); //true
System.out.println(a>=b); //false
System.out.println(a<=b); //true
System.out.println(a==b); //false
System.out.println(a!=b); //true
}
}
逻辑运算符
&(与):当运算符两边的表达式的结果都为true时,整个运算结果才为true,否则,只要有一方为false,则结果为false。
&&(短路与):即如果第一个表达式为false,则不再计算第二个表达式。&&的效率比&要高
|(或):当运算符两边的表达式的结果都为false时,整个运算结果才为false,否则,只要有一方为true,则结果为true。
||(短路或):即如果第一个表达式为true,则不再计算第二个表达式。||的效率比|的效率高
!(非):如果为真则变为假,如果是假则变为真
package com.example.operator;
/**
* Created by IntelliJ IDEA.
* Date: 2020/9/11
*/
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
// 与(and) 或(or) 非(not)
boolean a = true;
boolean b = false;
// ():括号里面先执行运算
System.out.println("a & b:"+(a&b));// false 逻辑与运算:两个变量都为真,结果才为true
System.out.println("a | b:"+(a|b));// true 逻辑或运算:两个变量有一个为真,结果才为true
System.out.println("! (a && b)"+!(a&&b));// true 如果为真则变为假,如果是假则变为真
System.out.println("----------分割线----------");
//短路与算法 假设前面为假就不执行后面运算了
int c = 5;
boolean d = (c<4) && (c++<4);
System.out.println(c); //5
System.out.println(d); //false
System.out.println("----------分割线----------");
//短路非算法 假设前面为真就不执行后面运算了
int e = 5;
boolean f = (e>4) || (e++>6);
System.out.println(e); //5
System.out.println(f); //true
}
}
位运算符
package com.example.operator;
/**
* Created by IntelliJ IDEA.
* Date: 2020/9/11
*/
public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
A&B = 0000 1100 上下比较0 0得0,0 1得0,1 1得1
A|B = 0011 1101 上下比较0 0得0,0 1得1,1 1得1
A^B = 0011 0001 上下比较0 0得0,0 1得1,1 1得0
~B = 1111 0010 上下比较0 0得1,0 1得1,1 1得0
2*8 = 16 2*2*2*2
//位运算效率极高
<< *2
>> /2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3);
// 0100 0000 在2的基础上往左移五位
System.out.println(2<<5);
}
}
条件运算符
- 三元运算符(也称三目运算符)
package com.example.operator;
/**
* Created by IntelliJ IDEA.
* Date: 2020/9/12
*/
public class Demo08 {
public static void main(String[] args) {
//三元运算符
// x ? y : z
//如果x==true,则结果为y,否则结果为z
int score = 50;
String type = score < 60 ? "不及格":"及格"; //必须掌握
// if
System.out.println(type); //不及格
int a = 60;
String type1 = a > 50 ? "及格":"不及格";
System.out.println(type1); //及格
}
}
扩展赋值运算符
package com.example.operator;
import java.sql.SQLOutput;
/**
* Created by IntelliJ IDEA.
* Date: 2020/9/12
*/
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; //a = a+b
a-=b; //a = a-b
System.out.println(a); //30
System.out.println("----------分割线------");
//字符串连接 +
System.out.println(a+b); //30
System.out.println(""+a+b);//1020 只要有一方出现String(字符串类型)其他的都会转换为字符串类型
System.out.println(a+b+"");//30 运算在前面先执行运算再拼接字符串
}
}
- *=,/=就不做演示了,和+=,-=做法是一样的