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

operatorexplainexampleresult
+plus12.45f+1527.45
-reduce4.56-0.164.4
*ride5L*12.45f62.25
/except7/23
%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.

operatoreffectgive an exampleOperation dataresult
>Compare whether the left is greater than the right'a'>'b'Integer, floating point, characterfalse
<Compare whether the left is smaller than the right156<456Integer, floating point, charactertrue
==Compare whether the left is equal to the right'c'=='c'Basic data type, reference typetrue
>=Compare whether the left is greater than or equal to the right479>=426Integer, floating point, charactertrue
<=Is the comparison workshop less than or equal to the right12.45<=45.5Integer, floating point, charactertrue
!=Compare whether the left is not equal to the right'y'!='t'Basic data type, reference typetrue

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

operatormeaningusageCombination direction
&&,&Logic andop1&&op2From left to right
||Logical orop1||op2From left to right
!Logical non!opRight to left

Use logical operators for logical operations

Expression 1Expression 2Expression 3Expression 4Expression 5
truetruetruetruefalse
truefalsefalsetruefalse
truefalsefalsefalsetrue
truetruefalsetruetrue

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
00000000000000000000000000000101
11111111111111111111111111111111100

Convert to ↓

Binary representation of integer-4
00000000000000000000000000000100
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
00000000000000000000000000000011
00000000000000000000000000000110

Convert to ↓

Binary representation of integer 6
00000000000000000000000000000111
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
00000000000000000000000000000111

Convert to ↓

11111111111111111111111111111000
~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
00000000000000000000000000001010
00000000000000000000000000000011

Convert to ↓

Binary representation of integer 3
00000000000000000000000000001001
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

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

Tags: Java Back-end

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