Java Personal learning Note self-increment (pre-and post) assignment operation (extended assignment operation) logical operator

Source: Internet
Author: User

+ + self-increment front: First operation after the value a=3; B=++a; a=4; b=4;

+ + + self-increment: First take value after Operation A=3; b=a++; a=4; b=3;

--self-reducing pre-set: First operation after the value a=3; b=--a;a=2;b=2;

--Self-reduction after: the first value after the Operation A=3;b=a--;a=2;b=3;

The preceding is the first operation after the value is set after the value of the operation.

Assignment Operation +

Extended Assignment Operation + =,-+, *=,/=,%=

Short S = 3;

s=s+2; ①

s+=2; Ii

What's the difference between ① and ②?

1 is the compilation does not pass, because s = S +2, the integer 2 in Java by default is the type int, S + 2 will first be converted to the int type to calculate, obtained an int type result. Then the int type cannot be converted directly to short, so there is a compilation error.

2 is correct, + = is an extended assignment operation, so the essence is assignment. The assignment operation does not change the data type. In fact, what s+= do is to get the result of S + 2 first 5, and then assign the value 5 to S.

logical operators

& Logic and | Logical OR! Logical Non-

&& Short circuit with | | Short circuit or ^ logic XOR

"&" regardless of whether the left is true or false, the right side will be calculated. "&&" When the left is false, the right is no longer counted.

"|" Whether or not the left is true or false, the right side will be calculated. "| |" When the left is true, the right side is no longer counted.

^ xor or when both conditions are true or both are false, the result is false. The pursuit is "different".

int x = 1;

int Y=1;

if (x++==2 & ++y==2) {

x = 7;

}

System.out.println ("x=" +x+ ", y=" +y);

The result above is x=2,y=2 interpretation & operation, regardless of whether the left is true or false, the right side will participate in the operation. However, the rear-facing + + is the first value after the operation. That is, x + = = 2 is not true, at this time the X. 1

int x = 1,y = 1;

if (x++==2 && ++y==2) {

x = 7;

}

System.out.println ("x=" +x+ ", y=" +y);

The result above is x=2,y=1 because the && operation on the left is false and the right side is no longer involved in the operation. So the result is x=2 y=1 (++y = = 2 Not participating in the operation)

int x = 1,y = 1;

if (x++==1 | ++y==1) {

x = 7;

}

System.out.println ("x=" +x+ ", y=" +y);

The result above is x=7,y=2 because | operations, regardless of the left is true false right are involved in the operation, so ++y = = 1 to participate in the operation. At this point the if condition is set to execute x = 7.

int x = 1,y = 1;

if (x++==1 | | ++y==1) {

x = 7;

}

System.out.println ("x=" +x+ ", y=" +y);

The result above is X=7,y=1, and X + + is back-mounted. The value is evaluated first. So x + + = 1 was established. Because | | Operation, when the left is true, the right side is no longer involved in the operation. So ++y==1 does not execute. However, the operation has already been established. So x=7, Y=1.

Java Personal learning Note self-increment (pre-and post) assignment operation (extended assignment operation) 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.