"Java Programming Ideas - Chapter 3 (Operators)"

operator

At the lowest level, data in java is manipulated by using operators.

3.2 Operators

An operator takes one or more arguments and produces a new value.

3.3 Priority

When there are multiple operators in an expression, the precedence of the operators determines the order in which the parts are evaluated. Multiply and divide first, then add and subtract.

3.4 Assignment

Basic data type assignment: is to assign the actual value to another variable.
Object assignment: Assignment to another variable is a reference, two references point to the same object.

3.5 Arithmetic Operators

+,-,*.%.

3.6 Auto increment and decrement

Prefix form: Calculation is performed before the value is generated.
Postfix form: the value is generated first, and then the operation is performed.

int i = 2;
System.out.println("i:"+i);      //i:2
//前缀
System.out.println("++i:"+ ++i); //++i:3
System.out.println("i:"+i);      //i:3
//后缀
System.out.println("i++:"+ i++); //i++:3
System.out.println("i:"+i);      //i:4

3.7 Relational Operators

The relational operator produces a boolean result. It computes the relationship between operand values, and the relationship expression yields true if the relationship is true, and false if the relationship is not.

Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2); //false 比较引用
System.out.println(n1 != n2);//true
System.out.println(n1.equals(n2));//true Integer的equals方法进行了拆箱处理。

Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;

System.out.println(f1 == f2);//true // Integer 缓存了在-128127之间的数
System.out.println(f3 == f4);//false

The equals method compares references by default.

3.8 Logical Operators

The logical operators and (&&), or (||), and not (!) can generate a Boolean value according to the logical relationship of the parameters.
Short circuit: Once the value of the entire expression can be unambiguously determined, the rest of the expression does not need to be evaluated.

3.12 Ternary Operators

boolean-exp ? value0 : value1

If the boolean-exp result is true, value0 is calculated, otherwise value1 is calculated.

3.13 Character Operators + and +=

The main function is: connect different strings.

3.15 Type Conversion Operators

Narrowing conversion: Convert a data type that can hold information to a data type that cannot hold that much information. Precision may be lost.
Widening conversion: In contrast to narrowing conversion, no precision is lost.

Except for boolean, any primitive data type can be type-converted to other primitive types.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476494&siteId=291194637