java 算术运算

不同类型数相加

long a = 12312132132L;
int b = 123;
char c = 10;
byte d = 8;
System.out.println(a+b+c+d); //long
System.out.println(b+c+d);  //int
System.out.println(c+d);  //int

如果操作数中有一个为long ,那么结果就为long

有double 就换成double

其他的都是int

int a = 10;
int b = 20;
System.out.println(""+a+b); //1020
System.out.println(a+b+“”); //30

为什么?

如果“+”号两边中的任意一边出现字符或字符串类型,则表示连接字符串。

字符串在前面,从左到右运算法则,“ ”+a也是字符串,再+b。

字符串在后面时,根据运算法则,从左到右,先运算a+b=30,然后再连接字符串。

运算符

短路运算,没有意义

&&两边都为真才是真,且左边为假的话不比较后面

&两边都是真才是真,左边为假也要比较右边

int c = 10;
boolean d = (c<4)&&(c++<4);
System.out.println(d);  //false
System.out.println(c);  //10
int c = 10;
boolean d = (c<4)&(c++<4);
System.out.println(d);  //false
System.out.println(c); //11

位运算

/*
A = 0011 1100
B = 0000 1101
A&B 0000 1100 对应位都为1才是1否则是0   与
A|B 0011 1101 对应位都是0才是0否则是1   或
A^B 0011 0011 对应位都相等才是1否则是0  异或
~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 1000       8
0001 0000       16
 */
System.out.println(2<<3);  //16

三元

x?y:z

if(x==true)
    return y;
else
    return z;
posted @ 2022-04-08 22:00  有情才能相守  阅读(117)  评论(0)    收藏  举报