Java支持以下运算符

目录

1. 算数运算符
2. 赋值运算符
3. 关系运算符
4. 逻辑运算符
5. 位运算符
6. 条件运算符
7. 扩展赋值运算符

  • 算数运算符:+,-,*,/,%,++,–
  • 二元运算符
//二元运算符(需要两个数运算称为二元运算符)
        //CTRL+D可以将内容复制下一行,非常好用!
        int a = 10;
        int b = 20;
        int c = 30;
        int d = 40;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/b);//0
        //最后一行的运算会有些问题 10/20的结果是0.5,那为什么这边的输出的结果是0呢?
        //因为啊a和b都是int类型把有小数点的运算结果向下取整,所以结果为0,所以这边运算结果精度有点缺失。
        //将变量强制转换就可以解决
        System.out.println(a/(double)b);//0.5
		//取余.模运算
        System.out.println(d%b);//0  40%20 运算结果为2,余数为0

注意:当不同数据类型运算时,有long类型存在时,运算结果为long类型。

没有long类型存在时,int,short,byte运算结果都为int类型。同理可得浮点数类型也是如此。

//二元运算的机制
        long a = 1234567891L;
        int b = 123;
        short c = 18;
        byte d = 10;

        double e = 3.14;
        float f = 3.14F;

        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
        //当不同数据运算时,有long类型时,数据结果就是long类型。当没有long类型时,byte、		   short、int类型运算时,结果为int
        //以上同理可得 浮点数运算时也是同一个道理
        System.out.println(e+f);//double

++(自增),–(自减):

//++(自增) , --(自减) 一元运算
        int a = 3;
        int b = a++;//在执行完这行代码后,先赋值给b,再自增
        // a = a+1 相当于隐藏这行代码
        System.out.println(a);//4

        int c = ++a;//在执行完这行代码后,先自增,在赋值给c

        System.out.println(a);//5
        System.out.println(b);//3
        System.out.println(c);//5

		//自减也是相同的意思。

在java运算中,很多要用到数学工具类来操作 如Math

//幂运算 3*3*3=27
		double pow =Math.pow(3,3);
        System.out.println(pow);
  • 赋值运算符:=
int a = 10;//将10这个值赋值给a这个变量
  • 关系运算符:> , < , >= , <=, == , !=
//关系运算符,返回结果boolean :true/false
    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
  • 逻辑运算符:&&,||,!
//逻辑运算符 &&(与 and), || (或 or) ,!(非 取反)
boolean a = true;
boolean b = false;
a&&b: //逻辑与运算:当两个值为真时,返回值是true
a||b://逻辑或运算:当两个值中的一个值为真时。返回值是true
!(a&&b)://取反,当为真时返回false;当为假时返回true

//短路运算
//当逻辑与运算,左边的值为false时,就会停止运算,返回false
int c = 5;
boolean d =(5<4)&&(c++<4);
//实验:当逻辑与运算左边值为false时,停止运算返回值位false。如果没有停止运算则c自增值为6。来实验是否左边值为false值时,会停止运算!
System.out.println(d);//false
System.out.println(c);//5

扩展 字符串连接符:+ 这是一个细节!!!

int a1 = 10;
int b2= 20;
System.out.println(a1+b2);//30
System.out.println(""+a1+b2);//1020 :字符串在前面则进行拼接
System.out.println(a1+b2+"");//30 :字符串在后面依旧进行运算
  • 位运算符:& , | , ^ , ~,>> , << , >>>(只需要理解!)
/*位运算使用再二进制上使用:& , | , ^ , ~
A=0011 1100
B=0000 1101
---------------------
A&B = 0000 1100  与:当对应位为1相同时返回为:1,其余返回为:0
A|B = 0011 1101  或:当对应位为0相同时返回为:0,对应位为1或者相同时,返回为:1
A^B = 0011 0001  异或:当对应位相同时返回为:0,不相同时返回为:1
 ~B = 1111 0010  取反:数值相反
 
 还可以这样理解将0代表false,1代表true,再将逻辑运算符的概念引进来
-------------------------
位运算符:>> ,  << 

2*8=16 可以拆解为2*2*2*2,但计算机这样算就很慢,所以换成位运算
0000 0000  0  二进制的8个代表着0,逢2近1
0000 0001  1
0000 0010  2
0000 0011  3
0000 0100  4
0000 1000  8
0001 0000  16   
//<<左移 *2
//>>右移 /2
System.out.println(2<<3);(0000 0010  2)1往前3次,(0001 0000  16)就位移到这个数
  • 条件运算符:? :
//条件运算符 三元运算  ? :
//x?y:z 如果x=true,则运行y结果,否则运行z结果

double score = 50;
String type =score < 60 ? "不及格" : "及格";
System.out.println(type);
  • 扩展赋值运算符:+= , -= , *= ,/=
//扩展赋值运算符 += -= 。。。。。
int a = 10;
int b = 20;

a-=b;//a = a-b
a*=b;//a = a*b
a/=b;//a = a/b