Java operator power node

Java operators are divided into four categories: arithmetic operators, relational operators, logical operators and bitwise operators. Arithmetic operator (9): + - * /% + + -- relational operator (6): = =! = > > = <<= Logical operator (6): & & ||! ^ &| Bitwise operator (7): & | ~ ^ > > < > >

Java basic data type:

Numeric type: integer: byte, short, int, long non integer: double, float non numeric type: char [character], Boolean [Boolean] I: arithmetic operator: Note: the operand of arithmetic operator must be numeric type. It is divided into unary operator and binary operator; Unary operator with only one operand; A binary operator has two operands, and the operator is between two operands. Unary operators: positive '+', negative '-', self addition '+', self subtraction '--' these four. ① "+ +" and "--" operators are only allowed to be used for variables of numerical type, not in expressions; "+ +" and "--" can be used before or after numerical variables; the difference between the two places is used: "+ +" and "--" before being used for numerical variables, in assignment operation, the "+ +" or "-- "The value of the operation variable shall be increased by 1 or decreased by 1 first, and then other operations shall be carried out;" + + "and" -- "are used for numerical variables. In the assignment operation, the value of the operation variable by" + + "or" -- "shall be used for other operations, and then its value shall be increased by 1 or decreased by 1.

The output result is:

b=5,a=5 c=-5,a=5 d=3,l=3 f=3,m=4 g=3,n=3 h=6,o=5

Note: there is a space between the unary operator and the operands before and after it. Errors may occur when compiling under some compilers. ② Binary operator, add '+', subtract '-', multiply '*', divide '/', and find the remainder '%'. In the arithmetic operator, ”+"," - "," * "," / "complete the four operations of addition, subtraction, multiplication and division,% is to find the remainder after the division of two operands. The operation rules are basically the same as the mathematical operation. In the arithmetic operation, the calculation is carried out in the order from left to right, and the multiplication, division and remainder take precedence over addition and subtraction. The difference is that the multiplication operator in the program cannot be omitted and can be written as" y = 2x "in mathematics "And it must be written as" y = 2 * x "in the program. When the data types of the two operands of binary operation are different, the data type of the operation result is consistent with the data type of the operands involved in the operation with higher precision (or longer bits).

The output result is:

int /int :a/d=3 double/int:b/a=1.1766666666666665 float/int:c/a=0.56666666

Conversion principle:

Convert byte, short, int, long, float and double from low precision to high precision. Low precision to high precision will be automatically converted, while high precision to low precision requires type coercion.

Data length of computer: bit: a binary data 0 or 1, which is 1bit (bit); Byte: the unit of measurement of storage space, which is 1byte = 8bit; For example, varchar of the database is byte; 1. The values of Boolean variables are: true, false, 1 byte (8 bits) 2. Char data types: Unicode characters, 16 bits 3. Byte: one byte (8 bits) (- 128 ~ 127) (- 7th power of 2 to 7th power of 2 - 1) 4. Short: two bytes (16 bits) (- 32768 ~ 32767) (- 15th power of 2 to 15th power of 2 - 1) 5. Int: four bytes (32 bits) (one word length) (- 2147483648 ~ 2147483647) (- 2's 31st power to 2's 31st power - 1) 6. Long: eight bytes (64 bits) (- 922337203685474808 ~ 9223372036854774807) (- 2's 63rd power to 2's 63rd power - 1) 7. Float: four bytes (32 bits) (3.402823e + 38 ~ 1.401298e-45) (E + 38 is the 38th power multiplied by 10, and e-45 is the negative 45th power multiplied by 10) 8. Double: eight bytes (64 bits) (1.797693e + 308 ~ 4.9000000e-324) corresponding to the basic data class types of Java: integer, float, Boolean, character, double, short, byte and long. Note: ① in numerical calculation, the syntax phenomenon - "promotion", that is, after arithmetic operation of byte, short and char (data types lower than int), the result will be automatically promoted to int type; ② When two char type operations are performed, they are automatically converted to int type; When char is operated with other types, it will be automatically converted to int type first, and then other types will be automatically converted; ③ Arithmetic operations can add parentheses "()" to improve the priority, give priority to operations in parentheses, and then operate with other operators; ④ The operand variable must be assigned before arithmetic operation. Otherwise, a syntax error will be reported. 2: Relational operator: the relational operator is used to compare the size between two values, and its operation result is a value of logical type (Boolean boolean type). Equal to '= =', not equal to '! =', Greater than '>', greater than or equal to '> =', less than '<', less than or equal to '< =', the following codes are:

The output result is: 9.5 < 8: false 8.5 < = 8.5: true a ~ Z: 97 ~ 122 a ~ Z: 65 ~ 90 'a' < 'a': true

Note: Boolean type can only compare equal and unequal, not size; >= It means greater than or equal to, one of the two can be established, and the result is true, < = is the same; The symbol for judging equality is two equal signs, not one equal sign, which requires special care. In the actual code, values, variables and operation results can be directly involved in the comparison. However, in the program, in order to enhance readability, it is sometimes necessary to write the comparison separately. Comparison operator is the basis of data comparison in program design, and it is also the basis of many logic implementations. In program logic, we often judge how to execute subsequent programs by comparing certain conditions. 3: Logical operators:

The logical operator requires that the data type of the operand is logical, and its operation result is also a logical value. The data of logical operation and the operation result of logical operator are boolean type. Logical and '& &', logical or '|', logical not '!', Truth table of logical XOR '^', logical and '&', logical or '|' logical operators:

A and B are two logical variables of logical operation; The operation rules of the two logics and (& & and &) are basically the same, and the two logics or The operation rules of (| and |) are basically the same. The operation of & and | is to calculate all logical expressions, and the operation of & & and | has the function of short circuit calculation. For &, if the condition on the left is false, the value of the condition on the right will also be calculated, while for & & if the condition on the left is false, the condition on the right will not be calculated. This phenomenon is called short circuit phenomenon. The so-called short circuit Calculation means that the system calculates the logical expression from left to right. Once the calculation result has been determined, the calculation process will be terminated. For the & & operation, as long as the value at the left end of the operator is false, the final result is false regardless of whether the value at the right end of the operator is true or false. Therefore, once the system determines that the value at the left end of the & & operator is false, the system will terminate the subsequent calculation process; For the | operation, as long as the value at the left end of the operator is true, the final result is true regardless of whether the value at the right end of the operator is true or false. Therefore, once the system determines that the value at the left end of the | operator is true, the system will terminate the subsequent calculation process. Note: the same XOR is false, and the opposite is true. Use the short circuit phenomenon: use the & & and | operators in programming, and it is not recommended to use the & and | operators.

4、 Bitwise operators:

Bit operation is an operation in binary bits, and its operands and operation results are integer values. Bit and '&', bit or '|', bit non '~', bit exclusive or '^', shift right '>', shift left '<', 0-filled shift right '>', bit and '&', bit or '|', bit non '~', bit exclusive or '^' are exactly the same as the truth table of the corresponding operation of the logical operation, except that the operands and results of the bit operation are binary integers, The operands and operation results of the corresponding operations of logical operations are of logical value boolean type.

Below are bit & operations:

int a = 15; // X equals 00001111 int of binary number, B = 6// Y equals 00000110 int of binary number C = x & Y / / Z equals 00000110 of binary number

The result is: the 00000 110 right shift of binary number is to shift a binary number to the right according to the specified number of bits, and the removed binary number will be discarded, The part moved in to the left is either complemented by 0 (when the number is positive) or 1 (when the number is negative). This is because integers are represented by complement inside the machine. The sign bit of positive numbers is 0 and the sign bit of negative numbers is 1. Moving a number to the left "< <" will multiply the value by the power of 2. Moving a number to the right > > "will divide the value by the power of 2. Moving it to the right (fill zero) operator, that is, move unsigned right, "> > >" will never produce a negative sign, because its sign bit is always filled with zero. No matter whether the number moved is positive or negative, the part moved to the left will be filled with 0.

Output: 8 1 int x = 70// X equals 01000110 int of binary number, y = 2; Int z = x > > y / / Z is equal to 00010001 of binary number, that is, Z is equal to 00010001 of binary number, that is, Z is equal to decimal number 17. int x = -70; // X equals 11000110 int of binary number, y = 2; Int z = x > > y / / Z is equal to 11101110 of binary number, that is, Z is equal to 11101110 of binary number, that is, Z is equal to decimal number - 18. Shift right and shift left operations are complement representations of integer machine numbers.

&The operator specifies that both signals a and B must be charged, and the result is charging. (1 means charged, 0 means not charged) | operator specifies that as long as signal a or B is charged, the output result is charged^ The XOR operator specifies that if one of signals a or B is charged, but signals a and B are not charged at the same time, the result is charged. ~ operator, also known as bitwise complement, flips all state of charge values.

5、 Simple use of assignment operators and other operators:

① Assignment operators can be combined with binary arithmetic operators, logical operators and bit operators to form simple operators, which can simplify the writing of some common expressions.

In program development, the distinction of "unary operator or shift operator" is widely used to simplify the writing of code, because it will increase the difficulty of reading code and make comments as much as possible.

② Square bracket [] and parenthesis () operators square bracket [] is an array operator, the value in square bracket [] is the subscript of the array, and the whole expression represents the element value of the subscript in the array. The parenthesis () operator is used to change the priority of the operator in the expression.

The output is: C ③ string plus (+) operator. When the operand is a string, the plus (+) operator is used to combine two strings; when one side of the plus (+) operator is a string and the other side is a value, the machine will automatically convert the value into a string and connect it into a string.

The output is: aabbb555 ④ conditional operator (ternary operator) < expression 1 >< Expression 2 >: < expression 3 > calculates the value of < expression 1 > first. When the value of < expression 1 > is true, the value of < expression 2 > is taken as the value of the whole expression; When the value of < expression 1 > is false, the value of < expression 3 > is taken as the value of the whole expression.

Output: 132 ⑤ forced type converter forced type converter can convert the type of an expression to a specified data type

The output is: 5 ⑥ object operator instanceof object operator instanceof is used to test whether a specified object is an instance of a specified class (or its subclass). If so, it returns true, otherwise it returns false.

The output is: string is object class ⑦ dot operator dot operator "." It has two functions: one is to reference the members in the class, and the other is to indicate the hierarchy of the package.

The output is: string key = 1, value = a, key = 2, value = B, key = 3, value = C

In actual development, multiple operators may appear in one operator. When calculating, it is calculated according to the priority level. Operators with high level operate first, and operators with low level operate later

The priority of operator priority table is from top to bottom, and the level is from high to low.

This article is transferred from the Internet and published by the power node

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>