Take you through the five operators of Java
- 2021-10-27 07:33:28
- OfStack
1. Arithmetic operators
The symbols of arithmetic operators are usually: addition (+), subtraction (-), multiplication (*), division (/), remainder (%), self-increase (+ +), and self-decrease (-).
Using int type variables and int type variables to do division, the result is still int type;
Dividing with a constant of type double and a constant of type int yields a result of type double; The double type can be obtained by using the forced type double conversion
System.out.println(7 / 2);//3.0
System.out.println((double)(7 / 2)); // 3.0
System.out.println((double)7 / 2); // 3.5
Since there is division, then for the division of 0 this special number, Java program is how to deal with it, are all abnormal errors? We can perform try 1
// Exception generated: java.lang.ArithmeticException: / by zero
int i = 123 / 0;
System.out.println(i);
// Exception generated: java.lang.ArithmeticException: / by zero
int j = 0 / 0;
System.out.println(j);
double k = 123.45 / 2;
System.out.println(k); // 61.725
double x = 123.45 / 0;
System.out.println(x); // Infinity (Positive infinity)
double y = -123.45 / 0;
System.out.println(y); // -Infinity (Negative infinity)
double m = 0.0 / 0;
System.out.println(m); // NaN ( Not a number No 1 Number)
double n = -0.0 / 0;
System.out.println(n); // NaN ( Not a number No 1 Number)
float x = 123.45F / 0;
System.out.println(x); // Infinity (Positive infinity)
float y = -123.45F / 0;
System.out.println(y); // -Infinity (Negative infinity)
float m = 0.0F / 0;
System.out.println(m); // NaN ( Not a number No 1 Number)
float n = -0.0F / 0;
System.out.println(n); // NaN ( Not a number No 1 Number)
// Exception generated: java.lang.ArithmeticException: / by zero
int x = 11 % 0;
System.out.println(x);
double y = 11.0 % 0;
System.out.println(y); // NaN
Self-increasing operator
Use the self-increment operator alone: Whether + + is placed before or after the variable that needs to be incremented, the value of the variable will be incremented by 1 after the operation
The self-increment operator is not used alone: int j = i + +; //i: 3 j: 2 (1 first assigns the value of i to j, and then i adds itself (first assigns the value and then adds itself)); int j = + + i; //i: 3 j: 3 (first i does self-increment, and then assigns the value of i (after self-increment) to j (first self-increment and then assignment));
Here is an additional exercise:
int i = 2;
int j = 3;
j = i++ + ++i+i*6;
What is the final j? (Result: 30)
2. Assignment operator
Assignment operators: =, +=, -=, *=, /=,% =
This is nothing but the assignment operation, just note that for data of non-default data type, when using assignment operators with arithmetic operators, it will automatically cast the type, for example:
byte j = 5; j + = 6; //The next sentence is equivalent to the previous sentence j = (byte) (j + 6);
3. Comparison Operators
Comparison operator: = =,! =, > , < , > =, < =; In Java, = = is used to mean equality (content is equal)
4. Logical operators
Logical operators:
&
,
|
,
!
,
^
,
&&
,
||
& Logical AND, 1 is false, and the result is false
Logical OR, where one is true, the result is true
! Logically wrong, reversing black and white
^: Logical XOR, similar to magnet (true and false are true, true and false are false)
& & Logical AND, if the first variable or expression involved in the operation is false, the result is false, and other variables or expressions will not be executed; If multiple logics and participate in the operation, and so on;
Logical OR, if the first variable or expression involved in the operation is true, the result is true, and other variables or expressions will not be executed; If multiple logics and participate in the operation, and so on;
& And & & Difference: & & An operation with a short circuit, such as when the first expression is false, will not execute the second expression (if (s! = null & & s. equal ("")), if s is null & It will report an error; & Can be used in bitwise operators.
Exercises:
int i = 2;
int j = 3;
System.out.println((++i == 2) && (j++ == 3));
Q: What are the final values of i and j? (3 for i, 3 for j)
int i = 2;
int j = 3;
System.out.println((++i == 2) & (j++ == 3));
Q: What are the final values of i and j? (3 for i, 4 for j)
5. Bitwise operators
Bit operator:
&
,
|
,
~
,
^
,
|
0
,
>>>
,
<<
Rule: (In binary) the operation is a complement
& One of the same bits of the two numbers in which the operation is performed is 0, and the result of the operation of this bit is 0
Bit OR, 1 of the same 1 bit of two numbers being operated on is 1, and the result of this bit operation is 1
~: Bits are not, and the number of operations is inverted by bits
^: Bit XOR, in the same 1 bit of two numbers operated, two are the same as 0 and two are different as 1 (rule: one number performs Bit XOR operation with another number twice in succession to obtain its own value)
System.out.println(2 & 3); // 2
System.out.println(2 | 3); // 3
System.out.println(~2); // -3
System.out.println(2 ^ 3); // 1
System.out.println(2 ^ 3 ^ 3); // 2
/*
* 2:0000 0010
* 3:0000 0011
*
* 2
* Original code: 0000 0000 0000 0000 0000 0000 0000 0010
* Inverse code: 0000 0000 0000 0000 0000 0000 0000 0010
* Complements: 0000 0000 0000 0000 0000 0000 0000 0010
*
* ~2
* Complements: 1111 1111 1111 1111 1111 1111 1111 1101
* Inverse code: 1111 1111 1111 1111 1111 1111 1111 1100
* Original code: 1000 0000 0000 0000 0000 0000 0000 0011
*/
< < Move to the left, delete the high position on the left, and fill the low position on the right with 0
> > Move to the right, because the highest bit on the left is a sign bit, so it is necessary to distinguish between 0 and 1; The highest bit on the left side is 0, and the left side is filled with 0; The highest position on the left side is 1, and the left side complements 1 > > > Unsigned right shift, no matter whether the highest bit on the left side is 0 or 1, the left side is filled with 0
System.out.println(2 << 1); // 4
System.out.println(2 >> 1); // 1
System.out.println(-2 >> 1); // -1
System.out.println(-2 >>> 1); // 2147483647
/*
* -2:
* Original code: 1000 0000 0000 0000 0000 0000 0000 0010
* Inverse code: 1111 1111 1111 1111 1111 1111 1111 1101
* Complements: 1111 1111 1111 1111 1111 1111 1111 1110
*
* Right shift 1 Bit operation (operation complement)
*
* Complements: 1111 1111 1111 1111 1111 1111 1111 1111
* Inverse code: 1111 1111 1111 1111 1111 1111 1111 1110
* Original code: 1000 0000 0000 0000 0000 0000 0000 0001
*
* Unsigned right shift 1 Bit operation (operation complement)
*
* Complements: 0111 1111 1111 1111 1111 1111 1111 1111
* Inverse code: 0111 1111 1111 1111 1111 1111 1111 1111
* Original code: 0111 1111 1111 1111 1111 1111 1111 1111
*/
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!