Java operator knowledge consolidation

Java Operators

One of the most basic use of a computer is to perform mathematical operations, as a computer language, Java provides a rich set of operators to manipulate variables. We can put the operator into the following groups:

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Other Operators

First, arithmetic operators

Role and function of arithmetic operators as in mathematics.

Operators description
+ addition
- Subtraction
* multiplication
/ division
Modulo (remainder)
++ Self-energizing - the operand value is incremented by 1
Decrement - reducing the value of the operand 1

Example:

public class Test {

  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--) );
     // 查看  d++ 与 ++d 的不同
     System.out.println("d++   = " +  (d++) );
     System.out.println("++d   = " +  (++d) );
  }
} 

---结果:
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5
a++   = 10
a--   = 11
d++   = 25
++d   = 27

Second, relational operators

Operators description
== If the values ​​of the two operands are equal, it is equal condition is true.
!= If the value of the operands are not equal, not equal condition is true.
> If the value of the left operand is greater than right operand value, then the condition is true.
< If the value of the left operand is less than right operand value, then the condition is true.
> = If the value of the left operand is greater than or equal to right operand, then the condition is true.
<= If the value of the left operand is less than or equal to right operand, then the condition is true.

Example:

public class Test {

  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) );   
  	} 
}  
---结果:
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false 

Third, the bitwise operator

Operators description example
Bitwise AND operator, if and only if this bit is a bit in both operands are non-zero only when the result is 1. (A & B), to give 12, i.e., 0000 1100
| Bitwise OR operator, as long as a bit of the two operands have a non-zero result when this bit is set to 1. (A | B) to give 61, i.e., 0011

uncommonly used:

Operators description example
^ Bitwise XOR operator, a bit of the two operands are not the same result when the bit is set to 1. (A ^ B) to give 49, i.e., 00,110,001
Bitwise complement operator inverted every operand. (~ A) -61 obtained, i.e., 11,000,011
<< Bitwise left shift operator. Bitwise left left operand right operand the specified number of digits. A << 2 to give 240, i.e. 11,110,000
>> Bitwise right shift operator. The left operand by the right operand bits specify the right number of digits. I.e., obtain 15 A >> 2 1111
>>> Zero padding bit right shift operator. The median value of the left operand specified by the operand right RIGHT movement resulting vacancy is filled with zeros. I.e., obtain 15 A >>> 2 0000 1111

Example: **

public class Test {
  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 );
  }
} 
---结果:
a & b = 12
a | b = 61

Fourth, the logical operators

versus

Conditions 1 condition1 Conditions 2 condition2 value
1 1 1
1 0 0
0 1 0
0 0 0

or

Conditions 1 condition1 Conditions 2 condition2 value
1 1 1
1 0 1
0 1 1
0 0 0

non-

Conditions 1 condition1 value
1 0
0 1
public class Test {
  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));
  }
} 
---结果:
a && b = false
a || b = true
!(a && b) = true

Differences and ||, & and && in |

Logical operators have expressed

Short circuit the left operator && is false, not required to judge the genuineness of a direct result of the right false operation does not continue

|| shorting operator true left and right ends no further operation will not continue operation result is true

Fifth, the assignment operator

Operators description
= Simple assignment operator, the value of the right operand assigned to the left operand
+ = Was added and assignment operator, and that the left operand right operand assigned to the left operand addition
- = Save and assignment operator that the left and right operand subtraction operand assigned to the left operand
* = And assignment operator by which the left and right operand multiplication operand assigned to the left operand
/ = And assignment operator except that the left and right operands operand division assigned to the left operand
(%)= And assignment modulo operator that the left and right operand operand assigned to the left operand modulo

uncommonly used:

Operators description
<< = Left-shift assignment operator
>> = Right shift assignment operator
&= Bitwise AND assignment operator
^ = Bitwise XOR assignment operator
| = Bitwise OR assignment operator
public class Test {
  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 = a + b = 30
c += a  = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a  = 5

Sixth, the conditional operator (? :)

Conditional operator is also called ternary operator. The operator has three operands, and needs to determine the Boolean expressions.

condition? Results 1: 2 results;

Result of the condition must be boolean, true, false;

public class Test {
   public static void main(String args[]){
        int a , b;   
        a = 10;    
		b = (a == 1) ? 20: 30;    
		System.out.println( "Value of b is : " +  b );
   }
}
---结果:
Value of b is : 30

Conditions can be complex

boolean conditionOne = 5>3;
boolean conditionTwo = 5>2;
boolean conditionThree = 5>3;
int num = (conditionOne && (conditionTwo || conditionThree)) ? 1 : 2;

Seven, operator precedence

I tried to write a pithy formula:

Monocular relationship multiplication and division, logical trinocular after the assignment.

Monocular: unary + - (negative) ++ - like
multiplication and division: arithmetic unary * /% + -
is: displacement unary operator << >>
relationships: unary> <> = <= ==! =
logical: logical unary & | ^ && ||!
trinocular: trinocular unary A> B X: Y?
after: meaningless, Minato words only for

Assignment: Assignment =

在这里插入图片描述

发布了7 篇原创文章 · 获赞 7 · 访问量 441

Guess you like

Origin blog.csdn.net/qq_43658155/article/details/104755160