java operator pen questions
java operator pen questions
1. The difference between & and &&
& Considered a logical operation can be considered only as && Bitwise logical operation with
if two symbols are used as logical operators when the following differences
& two conditions are true before and after the final result is true
&& shorting and & normally performed with the same result
of the current surface conditions is false when the result of a short circuit behind the false && expression is not performed
2. The most efficient way to calculate 2 * 8 Results
00000010 * most efficient method << 2 3 3 equivalent to 2 multiplied by 2 to the power of
0,000,100,000,000,010
0,000,000,000,010,000 ----> 16
00000000 multiplier is just 2 power
00000000
00000010000--> 16
3. The two variables int a = 1; int b = 2; how to swap values of two variables
int a = 1;
int b = 2;
//方式一 采用一个中间变量空间
int c = a;
a = b;
b = c;
好处是比较容易理解 值也不会出现问题 不好在于产生了一个新的内存空间
//方式二 利用两个数字的和
a = a + b; //a空间存储的是两个元素的和3 b没有变化
b = a - b; //利用两个元素的和减原来的b 剩下的是原来的a b==1 a==3
a = a - b; //利用两个元素的和 减b(原来a的值1) 剩下原来b的值赋值给a b==1
/*好处是省略了一个新的空间
不好在于
第一个:相对来讲不是很容易理解
第二个:可能会在+产生值越界
//方式三
a=a ^ b; 1^2====> 001
010
011====>3^2 ====>
010
001===>1
1^2^2==>1 一个数字异或同一个数字两次 值不会改变
a==异或的中间值3 b==2
b=a ^ b;a==异或的中间值3 b==1;
a=a ^ b;3^1 a==2 b==1