Java Common operators

Source: Internet
Author: User
Tags bitwise

Java common operator Java, operator Auto increment and auto decrement

prefix : the operator is in front of the variable. The operation is performed first, and then the value is generated.
suffix : the operator is behind the variable. The value of the husband, and then the operation.

Relational operators

= =,! =, equals The default comparison is the object's reference, not the object's contents.

Ternary operator
Boolean-exp  ?  value0  :  value1

Boolean-exp value is true calculation Value0, value1 for false calculation.

Truncation and rounding

Converting a low type to a high type when converting float and double types in Java automatically intercepts the fractional portion.
such as float above= 2.7 (int) above = 2 automatically intercepts the decimal part.
Calling Math.Round (above) when rounding is processed can be rounded.

Plus operator

Java expression transformation rules are converted from low to High:
1, all the Byte,short,char type value in the calculation will be promoted to the int type;
2, if there is an operand is a long type, the result is long type;
3. If one of the operands is float type, the calculation result is float type;
4, if one operand is a double type, the result of the calculation is double type;
5 . Variables modified by fianl do not automatically change type, and when 2 final modifiers are manipulated, the result is converted according to the type of the left variable.

Bitwise operator SHIFT Left <<

The left shift operator << causes all bits of the specified value to shift to the left of the prescribed number of times.

1) Its general format is as follows: Value << num
NUM Specifies the number of bits to shift the value to move.
Left-shift rule remembers only one point: Discard the highest bit, 0 the minimum bit
If the number of bits moved exceeds the maximum number of digits of the type, the compiler will model the number of bits moved. If you move 33 bits to the int type, you actually move only the 33%32=1 bit.
2) Arithmetic rules
In binary form, all numbers are shifted to the left by the corresponding number of digits, the high position is removed (discarded), and the low vacancy is 0. When the left-hand operand is of type int, the 31st bit of each 1-bit shift is moved out and discarded, and when the left-hand operand is a long, the 63rd bit of each 1-bit move is removed and discarded. When the left-hand operand is byte and short, these types are automatically expanded to be of type int.
3) Mathematical significance
On the premise that the number is not overflow, for positive and negative numbers, the left one is the equivalent of multiplying by 2 by 1, and the left shift n is the equivalent of multiplying by 2 of the n-th square
4) Calculation process:
Example: 3 <<2 (3 for int type)
1) Convert 3 to binary number 0000 0000 0000 0000 0000 0000 0000 0011,
2) The number of the high (left) of the two 0 is shifted out, the other numbers are shifted to the left 2 bits, 3) in the low (right) of the two empty fill 0. The resulting result is 0000 0000 0000 0000 0000 0000 0000 1100, converted to decimal is 12.
The number of bits moved exceeds the maximum number of digits of the type,
If you move into a higher-order bit (31 or 63 bits), the value becomes negative. The following program illustrates this: Java code

//left shifting as a quick-to-multiply by 2. public class Multbytwo {public static void main(String args[]) {int i; int num =0Xffffffe;For  (i=0; i<4; i++) {num = num <<1; System. out. println(num); }     }}

The output of the program is as follows:
536870908 1073741816 2147483632-32
Note: n-bit binary, the highest bit is the sign bit, so the value range represented by -2^ (n-1)--2^ (n-1)-1, so the modulo is 2^ (n-1).

Shift Right >>

Right-shift operator << causes all bits of the specified value to move to the right by the required number of times.

1) Its general format is as follows: Value >> num
NUM Specifies the number of bits to shift the value to move.
The rule that moves right only remembers one point: the symbol bit is unchanged, the left side is the symbol bit
2) Operational rules:
In binary form, all numbers are shifted to the right by the corresponding number of digits, the lower position is removed (discarded), the high position of the empty fill sign bit, that is, positive 0, negative complement 1
These types are automatically expanded to int when the operands to the right move are of byte and short type.
For example, if the value to be removed is negative, each right shift is 1 on the left, and if the value to be moved is a positive number, each right shift is 0 on the left, which is called the sign bit extension (sign extension), which moves right
The symbol used to hold negative numbers during operation.
3) Mathematical significance
Move right one is equivalent to 2, and the right shift n is the equivalent of dividing by 2 of the n-th side.
4) Calculation process

One >>2 (11 for int type)
1) 11 in binary form: 0000 0000 0000 0000 0000 0000 0000 1011
2) Move the last two digits of the low position out, since the number is positive, so it is 0 at the high.
3) The final result is 0000 0000 0000 0000 0000 0000 0000 0010. Conversion to decimal is 3.

>> 2 (35 for INT type)
35 conversion to binary: 0000 0000 0000 0000 0000 0000 0010 0011
Move the last two digits of the low position out: 0000 0000 0000 0000 0000 0000 0000 1000 Convert to decimal: 8

5) do not retain symbols when moving to the right
The values after the right shift are bitwise AND operation with 0x0f so that any sign bit extension can be discarded so that the resulting value can be used as the subscript for the definition array, resulting in the hexadecimal character represented by the corresponding array element. For example
Java code

public class HexByte {public static public void main(String args[]) {char hex[] = {' 0 ',' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ',' 9 ',' A ',' B ',' C ',' d ',' E ',' F ''};     byte B = (byte) 0xf1; System.out.println ("B = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]); } }   

(b >> 4) & 0x0f: The binary form of B is: 1111 0001 4 digits are removed: 0000 1111 Bitwise AND operation: 0000 1111 to 10 in form: 15
B & 0x0f Operation process:
The binary form of B is: 1111 The binary form of the 0001 0x0f is: 0000 1111 bitwise AND operation: 0000 0001 to 10 in the form of: 1 Therefore, the output of the program is as follows: B = 0xf1

Unsigned Right Shift >>>

The unsigned Right shift operator >>> its general format is as follows: Value >>> num
NUM Specifies the number of bits to shift the value to move.
Unsigned right-shift rules only remember one point: ignoring the sign bit extension, the 0-bit unsigned right-shift operator >>> is only meaningful for 32-bit and 64-bit values

Java Common operators

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.