Operators, expressions and statements of Java program development and learning

Operators, expressions and statements of Java program development

(Study reference book: Java University Practical Course Third Edition)

One, operator

(1) Arithmetic operators : + (addition),-(subtraction), * (multiplication), / (division),% (remainder), ++ (increment),-(decrement). Arithmetic mixed operations retain the precision of the highest precision data type.

(2) Relational operators :> (greater than), <(less than), >= (greater than or equal to), <= (less than or equal to), == (equal to),! = (Not equal to).

(3) Logical operators : && (and), || (or),! (non).

(4) Instanceof operator : object instanceof class, if the object on the left is an object created by the class on the right, the result of the operation is true, otherwise it is false.

(5) Conditional operator : 表达式1?表达式2:表达式3
algorithm: when the value of expression 1 is true, the result of the entire expression is the value of expression 2, if the value of expression 1 is false, the result of the entire expression is expression 3 Value.

(6) Shift operator : used for binary bit operation, divided into left shift operation and right shift operation.
Left shift: 被移位数<<移位量
expression meaning: move all the bits of the binary representation of the shifted number to the left by n bits, and fill with 0 on the right.
Right shift: 被移位数>>移位量
Meaning: shift all bits of the shifted number to the right by n bits, discard the low-order bits shifted out on the right, and fill with 0 or 1 on the left (positive numbers are filled with 0, and negative numbers are filled with 1).

  • For byte and short type shift operations, the result of the operation is of type int;
  • For the shift operation a (shift symbol) n; if a is of type byte, short, int, the system always calculates m = n%32 first, and then performs a (shift symbol) m to get the result.
  • For the shift operation a (shift symbol) n; if a is of type long, the system always calculates m = n%64 first, and then performs a (shift symbol) m to get the result.

(7) Bit operator : perform a bitwise operation on the binary data of one or two integer data, and the result of the operation is an integer data.
Bitwise AND: &, algorithm: if a&bthe corresponding bits are all 1, then the bit of c is also 1; otherwise, all are 0.
Bitwise OR: |, algorithm: If a|bthe corresponding bits are all 0, the bit is also 0; otherwise, they are all 1.
Bitwise negation: ~, algorithm: if ~athe corresponding bit is opposite, it is the result.
Bitwise XOR: ^, algorithm: if the a^bsame, the result is 0, otherwise it is 1. (Two XOR operations on the same number are themselves)

  • Bitwise operators (except XOR) can also manipulate logical data: that is, treat 1 as true and 0 as false.
  • If there are expressions on both sides of the bit operator, the value of the expression will be calculated first, and then the bit operation will be performed.

Second, the statement

Java statements are divided into 5 categories:
(1) Method call statement : Objects can call methods in the class to produce behavior
(2) Expression statement : Add; at the end of the expression to form a statement.
(3) Compound statement : {and} enclose some statements to form a compound statement, also called a code block.
(4) Control statements : conditional branch statements, loop statements, and jump statements.
(5) package statement and import statement : related to classes and objects.

Guess you like

Origin blog.csdn.net/YCF8746/article/details/112384866