Java Operator Usage Summary (emphasis: self-increment, bitwise, and logical operations)

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

the Java operators include the following: arithmetic operators, comparison operators, bitwise operators, logical operators, assignment operators, and other operators. (The figure is from the network)

simple operators, it is not too much to introduce the use, you can test yourself. for assignment operations, you can simplify the code by combining arithmetic and bitwise operations.

        int a = 5;         // A = a + A;        // a = a-a;        // a = a/a;        // A = a% A;        // A = a & 6;         // .....

Understanding the self-increment decrement operator

the self-increment reduction is divided into pre-and post-position. For example: i++ ++i I----I. To be clear, the self-increment or decrement of the pre-and post-set is no different if it is not applied to the expression operation, and if you are involved in an expression, just remember: who executes first.

        int a = 0;        A// or ++a        //  a output 1
In the most common for loop, in an iterative condition, i++ and ++i are no different, because they are not used for expressions.

The pre-increment and post-increment are applied to the expression.

        int a = 5;        System.out.println (a///5, first print a value, then a-minus 1        //4        //5 , increment the A value first, and then print a value

More complex, better understanding of the difference between pre-and post-set:

        int a = 6;         int b = 8;         int c = ++a + B---a--+--b + b++ +--A;        System.out.printf (//  5,7,25

Using images to understand them will be more straightforward.

Then give one, as shown in the figure to understand the self-increment.

        int a = 6,b = 8;         int // parentheses are easier to read //         int c =--a + A---b--+ ++b + a++-b--;        // 5,7,6    }

In Java, bit operations are often used to shift operations. Therefore, according to the personal understanding to review this knowledge point, not too deep. If you are interested in the bitwise operation can go to other blogs search, the explanation is very detailed.
The operation of the shift operation is to translate the numbers on the basis of the binary, with left and right shift operations.
Give a code example:

Test right SHIFT operation//result is 1 and 0system.out.println (6>>2); 6 Right Shift 2-bit System.out.println (8>>5); 8 Move right 5 bit

Essentially binary operations, so we turn 6 and 8 into binary.

If you do not understand the binary can also use the computer's own calculator, change to the programmer to use it. But it is recommended to learn binary, and skilled with octal and hexadecimal conversion, will let you understand the principle of computer.

Go to the Chase:
Binary in 6
0000 0000 0110

0000 0000 0001//Shift right 2 bit

1 times 2 of the 0-square output 1

Binary in 8
0000 0000 1000
0000 0000 0000//Shift right 5 bit

0 Times 2 of the 0-square output 0

Test left shift operation
The result was 192system.out.println (4<<5); System.out.println (3<<6);

Binary in 4
0000 0100
1000 0000//Shift left 5-bit
1 times 2 of the 7-square output 128

Binary in 3
0000 0011
1100 0000//Shift left 6-bit

1 times 2 of 7 square + 1 times 2 6 output 192

The above is based on the binary translation of the way, there are trickery way, calculate 2 of the time is very rapid.

is: A few times to the left to multiply by 2 of the parties, the right to move a few divided by 2 of several Parties

Take 4<<5 as an example: 4 times 2 of 5 times 4 * 32 can quickly produce results 128

10>>2:10 divided by 2 of 2 square 10/4 = 2

To get 2 of the 10-time side only need 1<<10.

--------------------------------------------------------------------------------------------------------

With respect to the Java operator, there is no need to memorize precedence, and using the parentheses operator can improve readability.

Java Operator Usage Summary (emphasis: self-increment, bitwise, and logical operations)

Related Article

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.