Java operators (arithmetic, logical, bit, assignment, relation, condition...)

1. Introduction to operators

All program development is a digital processing game. There must be so-called processing modes for digital processing, and these operation modes are called operators. For addition, we must use the "+" operator to complete, and for operators, there is also a sequential relationship, that is, priority. For example, when we were in elementary school, our four mixed operations were multiplication and division followed by addition and subtraction.

For program development, a large number of basic operators are provided, and these operators also have their own priority order. For program development, it is not recommended to write very complicated calculations, such as the following program:

public class Hello{
	public static void main(String args[]){
	int x=0;
	int y=1;
	int z=x++/y++*++y+y++-y--;
	System.out.println(z);
	}
}

Can you see the result of z at a glance? Is it not possible? If you write this way during development, you will definitely be scolded, and now the development must go to the code as brief, clear and clear as possible, so avoid such complex calculations in the program.

2. Arithmetic operators

Arithmetic operators complete the four arithmetic operations of addition, subtraction, multiplication, and division in mathematics. Among them, the unary operators are: + (positive),-(negative), ++ (increment), - (increase); binocular Operators: + (addition),-(subtraction), * (multiplication), / (division),% (remainder); "/" has two meanings, real number division and division.

public class Hello{
	public static void main(String args[]){
	int x=8;
	int y=3;
	int z=x/y;
	System.out.println(z);
	}
}

The above, 8/3, it is an even division, so the result is 2.

Among the arithmetic operators, the most troublesome ones are ++ (addition) and - (decrement):

》++Variable, --Variable: first increase or decrease the variable, and then calculate the number;

》Variable++, Variable--: Use variables for calculation first, and then increase or decrease automatically;

For example, the following program:

public class Hello{
	public static void main(String args[]){
	int x=8;
	int y=3;
	int z=x+++--y;//x先参与运算,即x是8,y先减1再参与运算,即y是2,因此8+2=10
	System.out.println("Z="+z);
	System.out.println("X="+x);
	System.out.println("Y="+y);
	}
}

3. Relational operators

Relational operators are used for comparison operations between two data, generally there are 6 types: == (equal to), != (not equal to),> (greater than), <(less than), >= (greater than or equal to), <= (less than or equal), their operation result is of Boolean type.

public class Hello{
	public static void main(String args[]){
	int x=8;
	int y=3;
	if (x>=y){
		System.out.println("x大于等于y");
		}
	}
}

When you need to pay attention, character data can also participate in comparison operations, such as the following:

public class Hello{
	public static void main(String args[]){
	char x='你';
		System.out.println(x>=1);
	}
}

At this time, the result we get is true. This is because when the char variable and the int variable are operated, they will be converted into an int variable for comparison, and the int variable of the character "you" is its code, which is a big Therefore, the number is greater than or equal to 1, so the result is true.

4. Bitwise operators

Bit operator is to perform operations on integers according to the binary bits. It is suitable for integer types and character types. There are seven bit operators: ~ (not), & (and), | (or), ^ (exclusive or), < <(Left shift), >> (right shift), >>> (unsigned shift), the following is the code of the bit operator:

public class Hello{
	public static void main(String args[]){
	int x=4;//二进制为:100
	x=x<<1;//左移1位,1000,其十进制为8
		System.out.println(x);
	}
}

Results of the

5. Logical operators

Logical operators operate on the value of Boolean type, and the result is still not Boolean type. There are 6 logical operators: & (and), | (or),! (Not), ^ (exclusive or), && (condition And), || (conditional or).

Among them, &&, || have the function of short-circuit calculation, that is, calculate whether the conditions are in the city from left to right one by one from the Boolean expression. Once the result can be determined, it will terminate immediately without calculating the subsequent conditions, such as the following code:

public class Hello{
	public static void main(String args[]){
		int i=0;
		boolean x=(1==1)||((++i)==0);//进行逻辑条件或
		System.out.println("x="+x);
		System.out.println("i="+i);
		i=0;
		boolean y=(1==1)|((++i)==0);//进行逻辑或
		System.out.println("y="+y);
		System.out.println("i="+i);
	}
}

The results are as follows:

6. Assignment operator

The assignment operator is represented by an'=', and its function is to make the variable get the value. The order of assignment operation is from right to left, that is, the value of the expression is calculated first, and then the variable obtains the result of the expression.

The important point is that assignment operators can form compound assignment operators with arithmetic, logic, and bit operators, forming some concise usage methods, such as' +=':

public class Hello{
	public static void main(String args[]){
		int x=6;
		x+=1;
		System.out.println("x="+x);
	}
}

Calculation result:

Using such a compound assignment operator can make our code look cleaner and more efficient.

7. Type coercion

The type coercion "()" coerces the data type of an expression to a specified data type, such as:

public class Hello{
	public static void main(String args[]){
		double x=6.63;
		int z=(int)x;//将double型的强制转换为int型
		System.out.println("z="+z);
	}
}

Calculation result:

8. Conditional operator

Conditional operator is the only ternary operator in Java, and its format is: expression 1? Expression 2: Expression 3, which means that if expression 1 is true, then expression 2 will be the result of the expression, otherwise, expression 3 will be the result of the expression.

public class Hello{
	public static void main(String args[]){
		double x=6.63,y=7.99,max;
		max=(x>y)?x:y;//条件运算符
		System.out.println("max="+max);
	}
}

Calculation result:

9. Bracket operator

The bracket operator "()" is used to change the precedence of operators in expressions.

10. String concatenation operator

The string concatenation operator "+" is used to concatenate two strings into one string.

11. Dot operator

The dot operator "." Is used to separate packages, sub-packages, classes, and class members.

12 object operators

The object operator instanceof is used to determine whether an object belongs to a specified class or its subclasses, and the result of the operation is a Boolean type.

13.new operator

The new operator is used to apply for the storage space of the array and create an object.

Guess you like

Origin blog.csdn.net/qq_25036849/article/details/108769199