基本运算符
运算符
Java语言支持如下运算符:
- 算术运算符:+,-,*,/,%,++,–
- 赋值运算符:=
- 关系运算符:>,<,>=,<=,==,!=
- 逻辑运算符:&&,||,!
- 位运算符:&,|,^,~,>>,<<,>>>(了解一下)
- 条件运算符: ? :
- 扩展赋值运算符:+=,-=,*=,/=
算术运算符
算术运算符:+,-,*,/,%,++,–
public class Demo1 {
public static void main(String[] args) {
//二元运算符
int a = 10;
int b = 20;
int c = 22;
System.out.println(a+b);//30
System.out.println(a-b);//-10
System.out.println(a*b);//200
System.out.println(a/(double)b);//0.5 如果不进行强转可能会出现小数结果只会取整
System.out.println(c%a);//2 取余 模运算
}
}
++,–
public class Demo4 {
public static void main(String[] args) {
// ++ 自增 -- 自减 一元运算符
int a = 5;
int b = a++;//执行完这行代码后,先给b赋值,再自增
//a = a + 1;
System.out.println(a);
//a = a + 1;
int c = ++a;//执行完这行代码前,先自增,再给c赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
拓展1
public class Demo2 {
public static void main(String[] args) {
long a = 111222L;
int b = 111;
short c = 10;
byte d = 5;
System.out.println(a+b+c+d);//Long 如果有一个数为long 结果也为long, double也是一样
System.out.println(b+c+d);//Int 如果没有long 结果都为int
System.out.println(c+d);//Int
}
}
拓展2
public class Demo2 {
public static void main(String[] args) {
//幂运算 2^3 2*2*2 = 8 很多运算,我们会使用一些工具来操作!
double pow = Math.pow(2, 3);//返回一个double类型
System.out.println(pow);//8.0
}
}
赋值运算符
赋值运算符:=
int a = 10;
关系运算符
关系运算符:>,<,>=,<=,==,!=
public class Demo3 {
public static void main(String[] args) {
//关系运算符返回的结果: 正确,错误 布尔值
int a = 10;
int b = 20;
System.out.println(a>b);//false
System.out.println(a<b);//true
System.out.println(a==b);//false
System.out.println(a!=b);//true
}
}
逻辑运算符
逻辑运算符:&&,||,!
public class Demo5 {
public static void main(String[] args) {
//与(&&) 或(||) 非(!)
boolean a = true;
boolean b = false;
System.out.println("a && b:"+(a&&b));//逻辑与运算:两个变量都为真,结果才为true
System.out.println("a || b:"+(a||b));//逻辑或运算:两个变量有一个为真,结果都为true
System.out.println("!(a && b):"+!(a&&b));//如果是true则为false,如果是false则为true
System.out.println("a && b:"+(b&&a));//短路运算 第一个已经为false,结果直接判断为false,后面的运算不做计算
//短路运算
int c = 5;
boolean d = (c<4)&&(c++>4);//第一个已经为false,结果直接判断为false,后面的运算不做计算
System.out.println(d);//false
System.out.println(c);//5
//对比
boolean e = (c++>4)&&(c<4);//第一个为true第二个为false结果为false,两个都进行了运算
System.out.println(d);//false
System.out.println(c);//6 第一个计算生效了
}
}
位运算符
位运算符:&,|,^,~,>>,<<,>>>
public class Demo6 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
-------------------
A&B = 0000 1100 //两位为1才为1,否则都为0
A|B = 0011 1101 //两位为0才为0,否则都为1
A^b = 0011 0001 //两位相同为0,否则为1
~B = 1111 0010 //完全相反
面试题:怎样计算2*8最快 ?
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);//16 2左移3位
System.out.println(2>>1);//1 2右移1位
}
}
条件运算符
条件运算符: ? :
public class Demo7 {
public static void main(String[] args) {
// x ? y : z
//如果x==true,则结果为y,否则结果为z
int score = 80;
String type = score < 60 ? "不及格" : "及格";
System.out.println(type);//及格
}
}
扩展赋值运算符
扩展赋值运算符:+=,-=,*=,/=
public class Demo8 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;//a = a+b a = 10+20
System.out.println(a);//30
a-=b;//a = a-b a = 30-20
System.out.println(a);//10
//!!拓展!!
//字符串连接符 +
//面试题:2个结果有什么不一样
//字符串拼接在运算前面则全部为String类型进行拼接,在运算后面则先计算后拼接字符串
System.out.println(""+a+b);//1020
System.out.println(a+b+"");//30
}
}