The "JAVA" operator and the logical operator

Source: Internet
Author: User
Tags arithmetic operators

1. Arithmetic operators

+: addition (+ + Plus)

-: Subtraction (---self-subtraction)

*: Multiplication

/: Division

%: Take the remainder operation

2. Relational operators

<: You can only compare relationships between basic types of data, and you cannot compare relationships between objects.

>: (Same relational operator "<")

<=: (Same relational operator "<")

>=: (Same relational operator "<")

= =: If you use this operator to compare references (variables) of two objects, you are essentially comparing two variables that refer to the same object. The so-called same object refers to whether it is an object stored in the same memory unit that was opened up in the stack (heap).

! =: (same relational operator "= =")

3, the logical operator (the operator can only be a Boolean type)

& Adnd (with) False&true = = False

&& (short circuit) False&&true = = False

| or (or) False|true = = True

|| OR (Short circuit) false| | True = = True

! Not (non)!true = = False

^ xor (xor) True^false = = True

Attention:

Logical operators are used to concatenate Boolean expressions. In Java different from the logical expression of mathematics 3<x<5, Java should be written in X>3 & X<5

    • The difference between "&" and "&&":

Single "&", on the left, whether true or false, on the right.

Double "&", left false, no calculation on the right.

    • "|" And the difference between "| |":

Single "|", on the left whether true or false, on the right side are calculated.

Two "|", the Left is true, the right side is not calculated.

    • "|" and "&" do bit operations:

Binary for | bit operation, only 0|0 time is 0, the other is 1, such as 6|5 first to convert 6 and 5 into binary. 6 binary for 110,5 binary is 101 then the bitwise operation 0|0 0 to have 1 1, calculated as 111,111 for the decimal 7, all 6|5 equals 7

Binary for the & bit operation, only 1&1 1, the other 0, the same can be 6&5 calculate the binary 100,100 into decimal 4

    • "^" XOR operation and "~" Inverse Code:

Any of the same binary XOR operations are 0,1^1=0,0^0=0, and different XOR operations are 1,1^0=1,0^1=1. The same as 6^5 011 then 011 of the decimal is 3. Prompts for a number XOR or two times to change back itself. Like 6^5^5 's 110,101,101. First get 011, then get 110 all back to the original value, this technique can be used to convert the value, do not need a 3rd variable can be two variable value of each other.

Anti-code: binary for the inverse code calculation. 1 becomes 0, 0 becomes 1. For example, the binary of the sub-~6,6 is 110 on the machine to make up 32 bits before 110 complement 0. Then the anti-code is 111 ..... 001, at this time the inverse code minus 1 to 111 ... 111000, then the original inverse code minus the subtracted code gets 0000 .... 000111 at this time the decimal is-7 then add the original minus 1 to get -7+1 is.

5. Shift operators (integer operators only)

Char, Byte, short type, before the shift, will be converted to type int, the result of the shift is also the int type; the operand to the right of the shift symbol intercepts only the last 5 bits of the second binary (the purpose is to prevent the shift operation from exceeding the int type's representation range: 2 5 is 32, The maximum range of type int is 32-bit), the long type is shifted, the result is still long, and the operator to the right of the shift symbol intercepts only the second binary of the latter 6 bits.

<<: Move Left

>>: Move right, insert 0 at highest bit if sign bit is positive, insert 1 at highest bit if sign bit is negative

>>>: Unsigned Right shift, no matter plus or minus, is inserted at the highest bit 0

Example of Shift:

public class Demo {public static void main (string[] args) {//0000 0000 0000 0101//0000 0000 0000 001 0 System.out.println (5>>1);//2//0000 0000 0000 +//0000 0000 0000 0100 System.out. println (8>>1);//4 System.out.println (3+5>>1);//4 System.out.println (5>>1);//5}}






The "JAVA" operator and the logical 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.