Fifth Java operator

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators integer division logical operators

First, arithmetic operators

Arithmetic operators we are learning from primary school, plus (+) minus (-) multiplication (*) in addition to (/) and modulus (%, integer division to obtain the remainder). Integer division removes decimals directly, rather than rounding, such as 10/3=3; floating-point numbers can also be division, but they are rounded up.

Java also operates in a shorthand form and assigns values at the same time. This is marked by an operator before the equal sign, and is fixed for all operators in the language, for example, in order to add 4 to the variable x and assign the result to X, available: x+=4;

Second, automatic increment and decrement

The decrement operator is "--" meaning the value minus-1, and the increment operator is "+ +" meaning the value +1. The two operators mainly note that they appear in the position of the variable, if it appears before the variable (--a or ++a), the calculation is performed first, and then the calculated value is assigned to the variable, if it appears after the variable (a--or a++), the variable is first assigned, and then perform the calculation operation.

int i = 1; System.out.println ("++i:" + ++i); Calculate the re-assignment System.out.println first ("i++:" + i++); The first assignment is to calculate System.out.println ("I:" +i); System.out.println ("I:" +-I.); Calculate the re-assignment System.out.println first ("i--:" + i--); The first assignment is to calculate System.out.println ("I:" +i);

Output Result:

++i:2i++:2i:3--i:2i--:2i:1
Third, logical operators

logical operators and (&&), or (| |) And not (!) Can generate a Boolean value (True or false). && | | is a two-dollar operator,&& as long as one of the values is false, the result is false,| | On the contrary, as long as there is a value of true, the result is true. is a unary operator, and if the value is true, the result is false.

The main note of the logical operator is that its "short-circuit",&& will not be executed as long as the expression following false is encountered. As long as the expression after the encounter is true, it will not be executed.

Boolean b = 1 > 2 && 1/0 ==0; 1>2 will not be executed for false,1/0. Boolean B1 = 1 < 2 && 1/0 ==0;//1<2 is true, execution 1/0 will be reported by zero exception Boolean b2 = 1 < 2 | | 1/0 = = 0; 1<2 for true,1/0 will not be executed. Boolean b3 = 1 > 2 | | 1/0 = = 0;//1>2 will be executed for false,1/0.
Four, bitwise operators

Bit operation is still relatively small in the project, I have seen the most exciting place to use the bitwise operation is the binary permission control.

Bitwise AND (&): If the value of two input bits is 1, the result is 1, otherwise the result is 0. Bitwise OR (|): If at least one of the two input bits is 1, the result is 1, and it generates a 0 only if the two input bits are 0. Bitwise XOR (^, XOR): If two input bit values are different, the result is 1. Bitwise NOT (--also called a "non" operator): is a unary operator, and if you enter 0, the result is 1, and if you enter 1, the result is 0.

It is worth noting that because the Boolean (Boolean) type value occupies one bit and true is 1,false 0, the Boolean type can also be bitwise and, or, and XOR, but not bitwise not (presumably to avoid confusion with logical not). The bitwise operator has the same effect as a logical operator when performing a Boolean type operation, except that it does not short-circuit.

Five, shift operators

The bitwise operand of the shift operator is the binary "bit", so the value should be converted to binary before the operation.

1. The left shift operator (<<) moves the operand to the left of the operator to the left by the number of bits specified by the operator (in the low 0). Right-shift operators fall into two categories: Signed right Shift (>>) and unsigned Right shift (>>>). "Signed" Right SHIFT, if the value is positive, then the high 0, if the value is negative, then the high 1. "Unsigned" shifts to the right, both positive and negative, in the high 0.

2. Char, Byte, and short before the shift operation, the value is automatically converted to int, and then the shift operation. Because there is a conversion process, the shifted value may not be the correct result. For example, the following:

Short s = -1;s >>>= 10; System.out.println (s); Output-1

The short type is automatically converted to the int type before the shift, negative numbers are in the form of complement in memory, so 1 in memory is 1111 1111 1111 1111 1111 1111 1111 1111, which becomes 10 0000 0000 0011 After unsigned shift 1 bit 111 1111 1111 1111 1111, because the short type only accounts for 2 bytes, 16 bits, so the lower 16 bits after the shift, or 1111 1111 1111 1111, so the result is-1.

3. The int type only occupies 32 bits, so if we move more than 32 digits, the value will never become 0 or 1, which is obviously wrong. So Java has made the specification, in order to prevent us from moving the impractical number in an int number, only the 5 low-level on the right is valid. This means that we first convert the shift number to binary, and then take the binary to the right of the 5 low, so that the number of shifts will never exceed 32. For example-1 >>>= 34,34 more than 32,34 binary is 00100010, take 5 low 00010, decimal 2, so-1 >>>= 34 equals-1 >>>= 2

int i = 1;i <<= 34; System.out.println (i);  4int i2 = 1;i2 <<= 2; System.out.println (I2); 4

4. Shift left one is quite multiplied by 2, the right shift one is quite divided by 2, and the displacement is the fastest of the 2 exponential operation.

5. Using SHIFT and bitwise arithmetic to convert integers of type int into binary, I think it's the quickest way

static void Pbinint (int i) {for (int j = 1; j >= 0; j--) {if ((<< j) & i)! = 0) {System.out.print (1);} Else{system.out.print (0);}}
Six or three USD if-else operator

The expression takes the following form: Boolean expression? Value 0: value 1

If the result of Boolean expression is true, the value 0 is evaluated and its result becomes the value that is ultimately generated by the operator. However, if the result of a Boolean expression is False, the value 1 is computed and its result becomes the value that is ultimately generated by the operator.

In fact, this operator is shorthand for if-else, and the effect is equivalent to the following statement

if (Boolean expression) {value 0;} else{value 1;}

Fifth Java operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.