运算符
-
-
算数运算符:+,-,*,/,%,++,--
-
赋值运算符 =
-
关系运算符:>,<,>=,<=,==,!=,instanceof(面向对象时候说)
-
-
位运算符:&,|,^,~,>>,<<,>>>(了解)
-
条件运算符? :(偷懒用的)
-
扩展赋值运算符:+=,-=,*=,/=(偷懒用的)
-
包机制




加减乘除
package operator;
public class Dome01 {
public static void main(String[] args) {
//二元运算符
//Ctrl + D:复制当前行到下一行
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);
}
}
紧急补充!!!
char,short,byte自动变形情况,
以下用byte举例:
-
byte a = 1;//a的类型为byte
-
byte b = 1+2;//b的类型为byte
-
byte c = a;//c的类型为byte
-
byte d = a +1;//报错
以下用char举例:
-
char e = 'a';//a的类型为char
-
char f = ‘a’ + 'b';//f的类型为char
-
char g = 'a' + 1;//g的类型为char
-
char i = e + 1;//报错//这里e是char类型
-
char j = a + 1;//报错//这里a是byte类型
以下用short举例:
-
short h = 321;//h为short类型
-
short k = 312 + 'a';//k为short类型
-
short u = k //u为short类型//k为short类型
-
short v = u + 1;//报错//u为short类型
-
short w = a + 1;//报错//a为byte类型
说明:以上结果均验证过,黑体部分请重点记忆!
关系运算符和取余
package operator;
public class Dome03 {
public static void main(String[] args) {
//关系运算符返回的结果:正确,错误 布尔值
//if(返回值为boolean类型){语句体;}
int a = 10;
int b = 20;
int c = 21;
//取余:也叫模运算
System.out.println(c % a);//c / a 21 / 10 = 2...1
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a == b);
System.out.println(a != b);
}
}

++--运算符
package operator;
public class Dome4 {
public static void main(String[] args) {
//++ -- 自增自减运算符
int a = 3;
int b = a++;//执行完这行代码后,先给b赋值,再自增
//a++ a = a +1
System.out.println(a);
//a = a + 1;
int c = ++a;//执行完这行代码前,先自增,再c赋值
int i = 1;
i = i++;
System.out.println(i);
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 2^3 2*2*2 = 8
//很多运算我们会使用一些工具来操作
double pow = Math.pow(3,2);//3^2=9.0
System.out.println(pow);
}
}

不重要的补充
int a = 21;
a = a++;
输出结果为21,不是22.
解释:
汇编里代码
1 movl $22,-4(%rbp)
2 movl -4(%rbp),%eax
3 leal 1(%rax),%edx
4 movl %edx,-4(%rbp)
5 movl %eax,-4(%rbp)
汇编基础:
eax,edx是累加寄存器和数据寄存器
rbp是堆栈基指针
%rbp叫内存地址
//立即数寻址 (数字--->寄存器)
解释:
-4(%rbp)对应变量a;
movl $22,-4(%rbp)
//将a的数值赋值到eax寄存器,此时eax里存22
movl -4(%rbp),%eax
//a+1的结果存到edx寄存器,此时edx里存23
leal 1(%rax),%edx
//将edx的数值赋值给a,也就是a=23.此时完成了a++。
movl %edx,-4(%rbp)
//将eax的数值赋值给a,也就是a自增之前的值赋值给a,也就是a=22.
另一种解释


逻辑运算符
package operator;
//逻辑运算符
public class Dome05 {
public static void main(String[] args) {
//与(and) 或(or) 非(取反)
boolean a = true;
boolean b = false;
//逻辑与运算:两个变量都为真,结果才为true
System.out.println("a && b:" + (a && b));
//逻辑或运算:两个变量有一个为真,则结果为true
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);
}
}

位运算符
package operator;
import java.sql.SQLOutput;
public class Dome06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
---------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~B = 1111 0010
2*8 = 16 //2*2*2*2
<<(左移)
>>(右移)
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 0101 5
0000 0110 6
0000 0111 7
0000 1000 8
0000 1001 9
0000 1010 10
0000 1011 11
0000 1100 12
0000 1101 13
0000 1110 14
0000 1111 15
0001 0000 16
*/
System.out.println(2<<3);//输出16
System.out.println(2<<4);//输出32
System.out.println(2<<5);//输出64
System.out.println(2<<6);//输出128
System.out.println(2<<7);//输出256
System.out.println(2<<8);//输出512
System.out.println(2<<9);//输出1024
System.out.println(2<<10);//输出2048
System.out.println(2<<11);//输出4096
System.out.println(2<<12);//输出8192
System.out.println(2<<13);//输出16384
System.out.println(2<<14);//输出32768
System.out.println(2<<15);//输出65536
System.out.println(2<<16);//输出131072
System.out.println(2<<17);//输出262144
//...
System.out.println(2<<29);//输出1073741824
System.out.println(2<<30);//输出-2147483648
//最多移动32次为一个周期
System.out.println(2<<31);//输出0
System.out.println(2<<32);//输出2
//<< *2
