Operators and expressions in Java

According to the functions of operators, there are the following types of basic operators.
Arithmetic operators +, -, *, /, %, ++,-
relational operators >, <, >=, <=, ==, !=
logical operators!, &&, ||, &, |, ^
Bitwise operators >>, <<, >>>, &, |, ^, ~
assignment operators =, extended assignment operators, such as +=, /=, etc.
Conditional operator?:
Other operators include component operator., subscript operator [], instance operator instanceof, memory allocation operator new, cast operator (type), method call operator (), etc.

One, arithmetic operators

+ - * /% + (take positive) - (take negative) ++ –

Two integers are "/" and the result is an integer. For the division and modulo operation between two integers, the formula (a/b)*b+(a%b)==a is always valid.

For the modulus operator "%", the operand can be a floating point number. That is, a% b has the same semantics as a-((int)(a/b)*b), which means that the result of a% b is the floating-point part remaining after the division. For example, 37.2%10=7.2. (Default double precision)

It is worth noting that the Java language has extended the addition operator to enable it to concatenate strings, such as "abc" + "de", to get the string "abcde".

Two, relational operators

Insert picture description here

Three, logical operators

!、&&、||、&、|、^(异或)

The relationship between logical operation and relational operation is very close. The result of relational operation is of Boolean type, and the operand and result of logical operation are of Boolean type.

The difference between concise operation (&&, ||) and non-concise operation (&, |) is: non-concise operation must calculate the left and right expressions before taking the result value; while concise operation may only calculate the left expression The expression on the right is not calculated, that is, for &&, as long as the left expression is false, the right expression is not calculated, and the entire expression is false; for ||, as long as the left expression is true, the right expression is not calculated , The entire expression is true.

Four, bit operator and assignment operator

1. Bitwise operators: >>, <<, >>>, &, |, ^, ~

2. Assignment operator: =, extended assignment operator (+= etc.)
Assignment operator features: automatic type conversion and right associativity

Five, conditional operators and string operators

1. Conditional operator: expression 1? Expression 2: expression 3

2. String operator The
string operator "+" is an operation performed on String. The operator "+" completes the string concatenation operation. If necessary, the system automatically converts the operand to String type.

float a=100.0//定义变量a为浮点型
print(“The value of a is”+a+“\n”)//系统自动将a转换成字符串
String s1+=a;//s1=s1+a,若a非String型,自动转换为String型。

Six, the precedence and associativity of expressions and operators

Insert picture description here

Guess you like

Origin blog.csdn.net/Bertil/article/details/108297472