An operator is a special notation used to represent data operations, assignments, comparisons, etc., with the following types of operators in Java
1. Arithmetic Operators
Javasupport for all basic arithmeticoperator, here are 7 basic arithmetic operators
+: addition operator, string connector
-: subtraction operator, negation operator
*: multiplication operator
/: Division operator
%: Seek remainder operator
+ +: self-adding operator
--: Self-decrement operator
self-adding and self-subtraction:
1) The self-add and decrement are the barrage operators, only one operand can be manipulated, only a single numeric variable can be manipulated, and the constant or expression cannot be manipulated.
2) The operator can appear to the left or right of the operand, and if the operator appears on the left, first increment or decrement the operand into an expression
If the operator appears to the right of the operand, then the expression operation is then self-increment self-decrement
Note the point:
1) Two particulars of the division operation. A. If the two operands are all integer types, the result is an integer, which is to truncate the result of the natural division.
For example 15/4 The result is 3, not 4.two operands are integer type, the divisor cannot be 0, otherwise an arithmetic exception is thrown。
B. Iftwo operands have one or two floating-point numbers, and the result is a floating-point number for natural division. And the allowable divisor can be 0 or 0.0 at this time, and the result is either positive infinity or negative infinity
2)The result of the calculation of the remainder is not necessarily an integer, but the result is the remaining value of the divisible result .。 Because the remainder operation is also a division operation, it follows the first particularity of the division operation.
But ifwhen two operands have more than one floating-point number, and the second value is 0 or 0.0, the result of the remainder is a non-number Nan. 0 or 0.0 for any number other than 0 will get 0 or 0.0;
Public Static voidMain (string[] args) {DoubleA = 5.2; DoubleB = 3.1; System.out.println (A%b); System.out.println ("The 5-to-0.0 redundancy result is:" +5%0.0); System.out.println ("The 0-to-5 redundancy result is:" +0%5); System.out.println ("The 0-to-5 redundancy result is:" +0%5); System.out.println ("The 0-to-0.0 redundancy result is:" +0%0.0); //The following statement throws an exception//System.out.println ("5 to 0 of the residual result is:" +5%0); intc = 5; intF = 5; intD = c+++3; intE = ++f+3; System.out.println (c); System.out.println (f); System.out.println (d); System.out.println (e); C= -C; System.out.println (c); }
Output:
2.1
5 to 0.0 the remainder result is: NaN
0 to 5 of the remainder result is: 0
0 to 5 of the remainder result is: 0
0 to 0.0 the remainder result is: NaN
6
6
8
9
-6
2. Assignment Operators
In Java, the "=" number is supported as an operator in the form of a loaded variable = Direct amount or variable, and Java supports continuous assignment, but low readability is not recommended
3. Comparison operators
comparison operators are typically used to compare the size of two variables or constants, and the returned result is a Boolean value of true or false
: Greater than sign. A variable or constant of a value type on the left and right side of the comparison operator is greater than the
: Less than sign. A variable or constant of a numeric type on the left and right side of the comparison operator is less than
>=: Greater than equals sign. A variable or constant of a numeric type on the left or right side of a comparison operator is greater than or equal to the
<=: Greater than equals sign. A variable or constant of a numeric type on the left or right side of the comparison operator is less than or equal to the
= = equals number. You can compare numeric types, Boolean types, and reference types. When only two reference types have a parent-child relationship
To be compared and to return true when pointing to the same object. When comparing numeric types, returns true as long as the values are equal.
For example ' A ' ==97 returns true
4. Logical operators
Logical operators operate on two variables or constants of a Boolean type
& &: With. The front and back two operands return true to return true
&: no short circuit with.
| | Or The front and back two operands return false to return false
|: no short circuit or.
! Non- There is only one operand. True non-operation for False,false is true for
^: XOR. The operand does not return true at the same time, and returns false
Note: for the difference between & and &&, a logical operation of two operands is performed, when the first operand is false,&& Operations on the right operand are not performed,
The second operand is shorted, but using & will still perform the operation of the second operand, and the second operand will not be shorted, but the result of the total return is the same as && .
| and | | The difference is explained in the same vein
Public Static voidMain (string[] args) {System.out.println ("The result of!false is" +!false); System.out.println (4>=5| | ' C ' > ' a '); System.out.println (4>=5^ ' C ' > ' a '); intA = 97; intb = 36; if(a>54 | | ++a>97) {System.out.println (a);//A does not have a self-added operation if(b>24 | ++b>36) {System.out.println (b);//B Get self-add arithmetic } } }
Output:
The result of!false is true
True
True
97
37
5. Bitwise operators
Java supports 7 bit operator:&,|,~,^,<<,>>,>>>. In general, bitwise operators can only manipulate variables or values of type integer
bitwise operators, are the computer's bottom-up complement Operation
&: Bitwise AND, Returns 1 when both digits are 1 o'clock
|: As long as one is 1 o'clock, return 1
~: Monocular operator, all of the bits of the binary code at the bottom of the computer are reversed by the number of operands
^: bitwise XOR, returns 0 when two bits are the same, 1
<<: Left-shift operator, 0 supplement after shift
>>: Right-shift operator, supplemented with the original symbol after shift, which is a negative number supplemented with 1, positive numbers supplemented with 0
>>>: Unsigned right-shift operator, always with 0 supplement after shift
rules followed by shift operations
1) for operands below int type (byte,char,short), Always automatic type conversion to int type after shift
2) for int type integer shift a>>b, when b>32, the system first balance B to 32 (int type value 32 bits),
The result is the number of moving bits, such as a>>33 and a>1 results exactly the same
3) for long type integer shift a>>b , when B>>64, is also the shift (B%64) bit
4) for << and >> shifts, shift the operand to the left N-bit, the result equals the operand multiplied by 2 of the N-th party,
Right shift n-bit results equals operand divided by 2 N, shift does not change the operand itself
Public Static voidMain (string[] args) {System.out.println ("5&9=" + (5&9)); System.out.println ("5|9=" + (5|9)); System.out.println ("~-5=" + (~-5)); System.out.println ("5^9=" + (5^9)); System.out.println (5<<2); System.out.println (-5<<2); System.out.println (-5>>2); System.out.println (-5>>>3); intA=6; intB=a>>3; System.out.println (A+ "+b+" after the right shift of 3 bits, the original operand is "+a+", it does not change itself "); }
Output:
5&9=1
5|9=13
~-5=4
5^9=12
20
-20
-2
536870911
6 The right Shift 3 bit is 0, the original operand is 6, it does not change itself
6. Three mesh operator
The trinocular operator has only one ? :The syntax format is as follows:(expression)? if-true Stament:if-false stament
int c=11; int d=12; System.out.println (c>d? ") C is greater than D ":(c = = d?" C equals D ":" C is less than D "));
Output: C less than D
7. Extended assignment operator
The assignment operators can be combined with arithmetic operators and shift operators to expand into more powerful operators. As long as you can use these extended operators, they are usually recommended, not only for better performance, but also for a more robust program.
+ =: For x+=y, equivalent to X=x+y;
-=: for x-=y, equivalent to x=x-y;
*=: For x*=y, equivalent to X=x*y;
/=: For x/=y, equivalent to x=x/y;
%=: For x%=y, equivalent to x=x%y;
&=: For X&=y, the equivalent of x=x&y;
|=: For x|=y, equivalent to X=x|y;
^=: For x^=y, equivalent to X=x^y;
<<=: For X<<=y, the equivalent of x=x<<y;
>>=: For X>>=y, the equivalent of x=x>>y;
^=: For x^=y, equivalent to X=x^y;
>>>=: For X>>>=y, the equivalent of x=x>>>y;
8. Operator Precedence
Operators in Java