基本运算符
-
算数运算符:+,-,*,/,%取余(模运算),++,- -
-
赋值运算符 = ,i=a,把a赋值给i
-
关系运算符:>大于,<,>=大于等于,<=,==等于,!=不等于,instanceof
-
逻辑运算符:&&,||,! 与或非,你和我,你或者我,不是你就是我
-
位运算符:&与,|或,^非,~取反,>>,<<,>>> 了解即可
-
条件运算符 ? :
-
扩展赋值运算符:+=,-=,*=,/=
Insert键可以切换光标
算数运算符:加减乘除
package operator;
public class Demo02 {
public static void main(String[] args) {
//二元运算符 两个数 a+b
//ctrl+D复制当前行到下一行
int a = 10;
int b = 20;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);//不加double会得0
//因为两个int计算得0.5后会舍弃小数0.5,故需加doule
}
}
不同类型的运算
package operator;
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);//long 没有long类型时,所有非int类型转为int类型
System.out.println(b+c+d);//int
System.out.println((double)c+d);//int
}
}
a++、a–、数学函数math
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增,自减 一元运算符
int a = 3;
int b = a++;//执行完这行代码后,先给b赋值,再自增
//a=a+1;
System.out.println(a);
//a++ a=a+1;
int c = ++a; //执行玩这行代码后,先自增,再给c赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算2^3=8 2*2*2 很多运算,我们会使用一些工具类来操作!
double pow = Math.pow(3,2); //math函数 3^2=9
System.out.println(pow);
}
}
关系运算符:大于、小于、取余
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果:正确,错误 这就是所谓的布尔值
//if语句
int a = 10;
int b = 20;
int c = 21;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
System.out.println(c%a);//21/10=2....1,取余得1
}
}
逻辑运算符:与或非、短路运算
package operator;
public class Demo05 {
public static void main(String[] args) {
// 与and 或or 非 取反
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));//如果是真则变为假,如果是假,则变为真
//短路运算 因为只有全部为真时才为真。所以只要发现一个为假则一定为假,后面的代码就短路不做运算。
//验证是否是短路运算
int c = 5; //把5赋值于c
boolean d = (c<4)&&(c++<4);//(c<4)为假,如果执行到这一步(c++<4),c会自增到6.
// 这里的<4是因为与运算符不能运算boolean+int类型.
System.out.println(d);//false
System.out.println(c);//结果为5,故此运算为短路运算
}
}
位运算符:二进制运算
package operator;
public class Demo06 {
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 = 16 2*2*2*2
二进制效率极高!! 计算机组成原理·
<<左移 每左移一位相当于*2
>>右移 每右移一位相当于/2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 0101 5
0000 1000 8
0001 0000 16
*/
}
}
条件运算符
package operator;
public class Demo08 {
public static void main(String[] args) {
// x ? y : z
//如果x==true,则结果为y,否则结果为z
int score =80;
String type = score <60 ?"不及格":"及格"; //如果成绩小于60分,就给type赋值不及格,否则赋值及格。(必须掌握)
//if 运算符的优先级
System.out.println(type);
//Insert键可以切换光标
}
}
扩展赋值运算符
package operator;
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);
System.out.println(a + b);//30
System.out.println("" + a + b);//1020
//字符串链接符 + ,在加号的两侧只要有一方出现了String类型(字符串类型),就会把另外的操作树都转化成String再进行链接
System.out.println(a + b + "");//30 如果String在后面,前面是正常运算。
}
}