Java introductory study notes (three)-operators and expressions, relational operators, logical operators, bit operators, other operators, the priority of operators

One, relational operators

Simple relational operators do not need to be explained, just practice on the code and it will be OK.

1. Code

float e=0;
		a=3;
		b=4;
		c=5;
		d=0;
		System.out.println("\n\na="+a+",b="+b+",c="+c+",d="+d+",e="+e);
		System.out.println("a>0:"+(a>0));
		System.out.println("b<c:"+(b<c));
		System.out.println("d<=0:"+(d<=0));
		System.out.println("b*b-4*a*c>=0:"+(b*b-4*a*c>=0));
		System.out.println("a!=b:"+(a!=b));
		System.out.println("d==e:"+(d==e));

Insert picture description here

2. Running results

Insert picture description here

Two, logical operators

There is nothing to say, just three,
&&
||
!
Here is a small exercise and I will take it in one stroke.

1. Code

		int year=2022;
		if( (year%4==0 && year%100!=0) || (year%400==0) || (year%3200==0 && year%172800==0))
			System.out.println("闰年");
		else
			System.out.println("非闰年");

2. Running results

Insert picture description here

Three, bitwise operators

Finally, something decent came.

1. Original code, inverse code and complement code

(1) Original code

Before the value, add the sign bit, 0 means positive, 1 means negative.

(2) Inverse code

The inverse code of a positive number is the same as the original code.
The inverse code of a negative number is the inverse of the original code, except for the sign bit.

(3) Complement code

In the computer, the value is always expressed in complement.
The reason is that the sign bit and the value bit can be processed uniformly by using the complement code.
At the same time, addition and subtraction can also be handled uniformly.
In addition, the complementary code and the original code are mutually converted, and the operation process is the same, and no additional hardware circuit is required.

The complement of a positive integer is the same as the original code.
The complement of a negative integer is its inverse plus 1.

2. Bit operator

Commonly used (familiar):
~ & | ^Uncommonly
used (a bit unfamiliar):
<<
>>
>>>

(1)>>n

Shifting to the right by n means that the leftmost complement sign bit means dividing by 2 to the power of n.

(2)<<n

Shifting to the left by n bits, and adding 0 to the right, means multiplying by 2 to the power of n.

(3)>>>n

Shift right unsigned by n, ignoring the sign bit, and all empty bits are filled with 0.

3. Example

Code

		b=-13;
		System.out.println("\n\n"+a+"的原码:0000 0110");
		System.out.println(""+a+"的反码:0000 0110");
		System.out.println(""+a+"的补码:0000 0110");
		
		System.out.println("\n\n"+b+"的原码:1000 1101");
		// 负数反码是其除了符号位以外的相反数
		System.out.println(""+b+"的反码:1111 0010");
		// 负数的补码是其反码加1
		System.out.println(""+b+"的补码:1111 0011");
		// 右移运算符,在最左边补符号位。
		System.out.println(""+b+">>2的补码:1111 1100(11)");
		System.out.println(""+a+"的补码:0000 0110");
		System.out.println(""+a+">>2的补码:0000 0001(10)");
		// 左移运算符,在最右边补0.
		System.out.println(""+a+"<<2的补码:0001 1000");
		System.out.println(""+b+"的补码:1111 0011");
		System.out.println(""+b+"<<2的补码:1100 1100");
		System.out.println(""+b+"<<2的反码:1100 1011");
		System.out.println(""+b+"<<2的原码:1011 0100");

operation result

Insert picture description here

Four, other operators

new, create an object or allocate space for the array
instanceof, determine whether the object is an instance of a class

Instance

		Integer aa=3;
		if(aa instanceof Integer)
			System.out.println(aa+"是整数");
		else
			System.out.println(aa+"不是整数");

Five, the precedence of operators

1. From left to right

.
()
[]

2. From right to left

+
-
++
- -
~
!

3. From left to right

*
/
%

4. From left to right

+
-

5. From left to right

<<
>>
>>>

6. From left to right

<
<=
>
>=
instanceof

7. From left to right (all the following 8-12)

==
!=

8、&

9、|

10、^

11、&&

12、||

13.? : (From right to left)

14. = (from right to left)

15. Mixed assignment operator (from right to left)

*=
/=
%=

+=
-=

<<=
>>=
>>>=

&=

|=

^=

Guess you like

Origin blog.csdn.net/qq_41563270/article/details/108703439
Recommended