# Getting started with Java operator

1, Assignment operator

The assignment operator "=" is a binary operator. Its function is to assign the value contained in the operand on the right to the operand of the workshop, such as

`int a = 100;`

The code is as follows

```package Number;

public class Eval {
public static void main(String[] args) {
int a,b,c;		//Declare int variable abc
a=15;		//Assign 15 to a
c=b=a+4;	//Assign the sum of a and 4 to b, and then assign it to c
System.out.println("c Value is:"+c);		//Output the value of variable C
System.out.println("c Value is:"+b);		//Output the value of variable B
}
}
```

2, Arithmetic operator

The arithmetic operators in java mainly include +, -, *, /,%, which are binary operators

 operator explain example result + plus 12.45f+15 27.45 - reduce 4.56-0.16 4.4 * ride 5L*12.45f 62.25 / except 7/2 3 % Seeking remainder 12%10 2

Tip: "+" and "-" can also be used as positive and negative symbols of data

The code is as follows

```package Number;

public class Arith {
public static void main(String[] args) {
float a=45.56f;
int b=152;
int c=24;
System.out.println("Hewei:"+(a+b));
System.out.println("Difference is:"+(a-b));
System.out.println("Product for:"+(a*+b));
System.out.println("Shang Wei:"+(a/b));
System.out.println("Yu Wei:"+(b%c));
}
}
```

3, Self increasing and self decreasing operators

Self increasing and self decreasing operators are unary operators, which can be placed before or after the operand. The operand must be an integer or floating-point variable.

Code examples are as follows

```++a(--a);        //Indicates that the value of a is increased (decreased) by 1 before using variable a
a++(a--);       //Indicates that after using the variable a, the value of a is increased (decreased) by 1```

Under analysis, the function of + + A and a + + is equivalent to a=a+1, assuming a=4

`b=++a;        //First, the value of a is + 1, and then assigned to b. at this time, the value of a is 5 and b is 5`

To put it another way, suppose a=4

`b=a++;        //First assign the value of a to b, and then change the value of a to 5. At this time, the value of a is 5 and the value of b is 4`

4, Comparison operator

The comparison operator is a binary operator, which is used for the comparison between variables, variables and independent variables and other types of information in the program.

 operator effect give an example Operation data result > Compare whether the left is greater than the right 'a'>'b' Integer, floating point, character false < Compare whether the left is smaller than the right 156<456 Integer, floating point, character true == Compare whether the left is equal to the right 'c'=='c' Basic data type, reference type true >= Compare whether the left is greater than or equal to the right 479>=426 Integer, floating point, character true <= Is the comparison workshop less than or equal to the right 12.45<=45.5 Integer, floating point, character true != Compare whether the left is not equal to the right 'y'!='t' Basic data type, reference type true

The code is as follows

```package Number;

public class Compare {
public static void main(String[] args) {
int a=4;		//
int b=5;		//
//Compare a and b in turn
System.out.println("a>b The return value of is:"+(a>b));
System.out.println("a<b The return value of is:"+(a<b));
System.out.println("a==b The return value of is:"+(a==b));
System.out.println("a!=b The return value of is:"+(a!=b));
System.out.println("a>=b The return value of is:"+(a>=b));
System.out.println("a<=b The return value of is:"+(a<=b));
}
}```

5, Logical operator

Expressions that return Boolean values, such as comparison operators, can be combined to form a more complex expression, which is realized by logical operators

 operator meaning usage Combination direction &&,& Logic and op1&&op2 From left to right || Logical or op1||op2 From left to right ! Logical non !op Right to left

Use logical operators for logical operations

 Expression 1 Expression 2 Expression 3 Expression 4 Expression 5 true true true true false true false false true false true false false false true true true false true true

The code is as follows

```package Number;

public class Calculation {
public static void main(String[] args) {
int a=2;
int b=5;
//Declare a boolean variable to save the return value after applying the logical operator "& &"
boolean result=((a>b)&&(a!=b));
//Declare a boolean variable to save the return value after applying the logical operator "‖"
boolean result2=((a>b)||(a!=b));
System.out.println(result);
System.out.println(result2);
}
}
```

6, Bitwise operator

Bitwise operators, in addition to bitwise AND and bitwise OR operators, are used to handle operands of integers

1. "Bitwise and" operation

The operator of bitwise and is "&", which is a binocular operator. The algorithm of bitwise and operation is: if the corresponding bits of two integer data a and b are 1, the result bit is 1, otherwise it is 0

 Binary representation of integer 5 00000000 00000000 00000000 00000101 111111111 111111111 111111111 11111100

Convert to ↓

 Binary representation of integer-4 00000000 00000000 00000000 00000100 The result of 5 & - 4, the decimal number is 4

2. "Bitwise or"

The bitwise OR operator is "|" and is a binocular operator. The algorithm of bitwise OR operation is: if the corresponding bits of two integer data a and b are 0, the result bit is 0, otherwise it is 1

 Binary representation of integer 3 00000000 00000000 00000000 00000011 00000000 00000000 00000000 00000110

Convert to ↓

 Binary representation of integer 6 00000000 00000000 00000000 00000111 The result of 3|6, decimal represents 7

3. "Bitwise negation" operation

"Bitwise negation" operation is also called "bitwise non" operation. The operator is "~", which is a unary operator. "Bitwise negation" is to change the 1 in the operand binary to 0 and 0 to 1

 Binary representation of integer 7 00000000 00000000 00000000 00000111

Convert to ↓

 11111111 11111111 11111111 11111000 ~Binary representation of 7, decimal - 8

4. "Bitwise XOR" operation

The operator of bitwise XOR operation is "Λ", which is a binocular operator. The algorithm of bitwise XOR operation is that when the binary representation of two operands is the same (both 0 and 1), the result bit is 0, otherwise it is 1

 Binary representation of integer 10 00000000 00000000 00000000 00001010 00000000 00000000 00000000 00000011

Convert to ↓

 Binary representation of integer 3 00000000 00000000 00000000 00001001 The result of 10 Λ 3, decimal represents 9

5. Shift operation

< <: move left

>>: shift right

>>>: move right without symbol

Move left is to move the binary data of the operand on the left of the operator to the left according to the number of bits specified by the operand on the right of the operator, and move right to fill the empty part with 0. Moving right is more complex. When the "> >" symbol is used, if the highest bit is 0, the empty bit shifted to the right will be filled with 0; if the highest bit is 1, the empty bit shifted to the right will be filled with 1

7, Ternary operator

Use format of ternary operator

`Conditional mode? Value 1: value 2`

Algorithm of ternary operator: if the value of conditional expression is true, the whole expression takes 1; otherwise, it takes 2

```boolean b=20<45?true:false;
```

if.....else statement equivalent to ternary operator

The code is as follows

```boolean a;  //Declare boolean variables
if(20<45):  //Take 20 < 45 as the judgment condition
a=true  //If the condition holds, assign true to a
else:
a=false //If the condition does not hold, assign false to a```

8, Operator priority

 priority describe operator 1 brackets () 2 Sign +,- 3 Unary operator ++,--,! 4 Multiplication and division *,/,% 5 Addition and subtraction +,- 6 Shift operation >>,>>>,<< 7 Compare size <,>,>=,<= 8 Compare for equality ==,!= 9 Bitwise sum operation & 10 Press to XOR ∧ 11 Bitwise OR operation | 12 Logic and operation && 13 Logic and operation || 14 Ternary operator ？: 15 Assignment Operators =

Tags: Java Back-end

Posted on Sat, 04 Dec 2021 15:25:43 -0500 by dc2_jspec