Introduction:
Operator, also known as operator, is a symbol used to realize the functions of assignment, comparison and arithmetic operation. This paper mainly introduces the use of operators (arithmetic operator, relational operator, assignment operator, logical operator and bit operator) in Java and related sample code.
1. Java operator
Operators are used to perform operations on variables and values.
In the following example, we use the + operator to add two values together:
For example:
int x = 100 + 50;
Although the + operator is usually used to add two values together, for example, in the above example, it can also be used to add a variable to a value or a variable to another variable:
For example:
int sum1 = 100 + 50; // 150 (100 + 50) int sum2 = sum1 + 250; // 400 (150 + 250) int sum3 = sum2 + sum2; // 800 (400 + 400)
Java divides operators into the following groups:
- Arithmetic operator
- Assignment Operators
- Relational operator
- Logical operator
- Bitwise Operators
2. Arithmetic operator
Arithmetic operators are used to perform common mathematical operations. Arithmetic operators are used in mathematical expressions. They play the same role as in mathematics. The following table lists all arithmetic operators.
Operator | describe | example |
---|---|---|
+ | Values on both sides of the addition operator | a + b equals 30 |
- | Subtraction - left operand minus right operand | a – b equals - 10 |
* | Values on both sides of the multiply multiply operator | a * b equals 200 |
/ | Division - left operand divided by right operand | b / a equals 2 |
% | Remainder - the remainder of the left operand divided by the right operand | B% a equals 0 |
++ | Auto increment: the value of the operand increases by 1 | b + + or + + b equals 21 |
- - | Self subtraction: the value of the operand is reduced by 1 | b -- or -- b equals 19 |
For example,
public class main { public static void main(String[] args) { int a = 10; int b = 20; int c = 25; int d = 25; System.out.println("a + b = " + (a + b) ); System.out.println("a - b = " + (a - b) ); System.out.println("a * b = " + (a * b) ); System.out.println("b / a = " + (b / a) ); System.out.println("b % a = " + (b % a) ); System.out.println("c % a = " + (c % a) ); System.out.println("a++ = " + (a++) ); System.out.println("a-- = " + (a--) ); // The difference between d + + and + + d System.out.println("d++ = " + (d++) ); System.out.println("++d = " + (++d) ); } }
**Note: * * prefix self increment and self subtraction (+ + D, – d) is to perform self increment or self subtraction operation first, and then expression operation. Suffix self addition and self subtraction (a++,a –) is to perform expression operation first, and then self addition or self subtraction operation.
3. Java assignment operator
Assignment operators are used to assign values to variables.
**For example: * * in the following example, we have the assignment operator (=) assign the value 10 to a value named x:
int x = 10;
The additional assignment operator (+) =) adds a value to the variable:
For example:
int x = 10; x += 5;
List of all assignment operators:
Operator | describe | example |
---|---|---|
= | A simple assignment operator that assigns the value of the right operand to the left operand | c = a + b assigns the value of a + b to c |
+= | The addition assignment operator, which adds the left and right operands and assigns them to the left operand | c += a is equivalent to c = c + a |
-= | Subtraction and assignment operator, which subtracts the left operand from the right operand and assigns the value to the left operand | c -= a is equivalent to c = c - a |
*= | The multiplication and assignment operator, which multiplies the left and right operands and assigns values to the left operand | c *= a is equivalent to c = c * a |
/= | The division and assignment operator, which divides the left and right operands and assigns them to the left operand | c /= a, c is equivalent to c = c / a when it is of the same type as a |
(%) = | Modulo and assignment operator, which assigns the left and right operands to the left operand after modulo | c% = a is equivalent to c = c% a |
<<= | Left shift assignment operator | C < < 2 is equivalent to C = C < < 2 |
>>= | Right shift assignment operator | C > > = 2 is equivalent to C = C > > 2 |
&= | Bitwise and assignment operators | c & 2 is equivalent to c = c & 2 |
^= | Bitwise XOR assignment operator | c ^= 2 is equivalent to c = c ^ 2 |
= | Bitwise or assignment operator | c |
For example,
public class main { public static void main(String[] args) { int a = 10; int b = 20; int c = 0; c = a + b; System.out.println("c = a + b = " + c ); c += a ; System.out.println("c += a = " + c ); c -= a ; System.out.println("c -= a = " + c ); c *= a ; System.out.println("c *= a = " + c ); a = 10; c = 15; c /= a ; System.out.println("c /= a = " + c ); a = 10; c = 15; c %= a ; System.out.println("c %= a = " + c ); c <<= 2 ; System.out.println("c <<= 2 = " + c ); c >>= 2 ; System.out.println("c >>= 2 = " + c ); c >>= 2 ; System.out.println("c >>= 2 = " + c ); c &= a ; System.out.println("c &= a = " + c ); c ^= a ; System.out.println("c ^= a = " + c ); c |= a ; System.out.println("c |= a = " + c ); } }
4. Java relational operators
Relational operators are used to compare two values:
Operator | describe | example |
---|---|---|
== | Judge whether the values of two operands are equal. If they are equal, the condition is true. | x == y |
!= | Judge whether the values of two operands are equal. If the values are not equal, the condition is true. | x!= y |
> | Judge whether the value of the left operand is greater than the value of the right operand. If so, the condition is true. | x> y |
< | Judge whether the value of the left operand is less than the value of the right operand. If so, the condition is true. | x |
>= | Judge whether the value of the left operand is greater than or equal to the value of the right operand. If so, the condition is true. | x> = y |
<= | Judge whether the value of the left operand is less than or equal to the value of the right operand. If so, the condition is true. | x <= y |
For example,
public class main { public static void main(String[] args) { int a = 10; int b = 20; System.out.println("a == b = " + (a == b) ); System.out.println("a != b = " + (a != b) ); System.out.println("a > b = " + (a > b) ); System.out.println("a < b = " + (a < b) ); System.out.println("b >= a = " + (b >= a) ); System.out.println("b <= a = " + (b <= a) ); } }
5. Java logical operators
Logical operators are used to determine the logic between variables or values:
Operator | Name | Description | example |
---|---|---|---|
&& | Logic and | Returns true if both statements are true | x <5 && x <10 |
! | Logical non | Inverts the result, and returns false if the result is true | !(x <5 && x <10) |
For example,
public class main { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("a && b = " + (a&&b)); System.out.println("a || b = " + (a||b) ); System.out.println("!(a && b) = " + !(a && b)); } }
Short circuit logic operator
When the and logical operator is used, the result is true only when both operands are true. However, when the first operation is false, the result must be false. At this time, the second operation will not be judged.
public class main { public static void main(String[] args){ int a = 4;//Define a variable; boolean b = (a<3)&&(a++<10); System.out.println("b = "+b); System.out.println("a = "+a); } }
6. Java bitwise operator
Java defines bit operators, which are applied to integer types (int), long, short, char, and byte.
Bit operators act on all bits and operate bitwise. For example, a = 30, b = 19; Their binary format representation will be as follows:
a = 0011 0000 b = 0001 1001 ----------------- a&b = 0001 0000 a | b = 0011 1001 a ^ b = 0010 1001 ~a= 1100 1111
Operator | describe | example |
---|---|---|
& | Bitwise and operator, if the corresponding bits are 1, the result is 1, otherwise it is 0 | (a&b) |
^ | XOR operator. If the corresponding bit values are the same, the result is 0, otherwise it is 1 | (a ^ b) |
〜 | The bitwise negation operator flips each bit of the operand, that is, 0 becomes 1 and 1 becomes 0. | (〜a) |
<< | Bitwise shift left operator, the left operand shifts the number of bits specified by the bitwise left right operand. | a << 2 |
>> | Bitwise shift right operator, left operand shifts the number of bits specified by the right operand bitwise. | a >> 2 |
>>> | Shift right by bit zero filling operator, the value of the left operand is shifted right by the number of bits specified by the right operand, and the empty bits obtained by moving are filled with zeros. | a >>>2 |
For example,
public class main { public static void main(String[] args) { int a = 60; /* 60 = 0011 1100 */ int b = 13; /* 13 = 0000 1101 */ int c = 0; c = a & b; /* 12 = 0000 1100 */ System.out.println("a & b = " + c ); c = a | b; /* 61 = 0011 1101 */ System.out.println("a | b = " + c ); c = a ^ b; /* 49 = 0011 0001 */ System.out.println("a ^ b = " + c ); c = ~a; /*-61 = 1100 0011 */ System.out.println("~a = " + c ); c = a << 2; /* 240 = 1111 0000 */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 15 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 15 = 0000 1111 */ System.out.println("a >>> 2 = " + c ); } }
To learn more about analysis and data capture, you can view:
http://cloud.yisurvey.com:9081/html/bd9fb365-ae11-4182-84d0-af28197bac71.html?ly=csdn
This article is reproduced from the Internet for learning and communication only. The copyright of the content belongs to the original author. Please contact us for deletion if there are works, copyright and other problems.
Special note: This article is for technical exchange. Please do not use the involved technology for illegal purposes, otherwise you will bear all the consequences. If you feel that we have violated your legitimate rights and interests, please contact us for handling