Java I ++ traps, java I traps

Source: Internet
Author: User

Java I ++ traps, java I traps

Interview traps:

Int I = 0; I = I ++;

At this time, the I value:

I is eventually 0;

When I = I ++ is executed, the priority of the value assignment symbol is the lowest. I ++ is first executed;

Execute I ++. The program first writes the I value 0 into the stack, and then performs operations on I ++;

After I ++, I is 1;

Finally, execute a value assignment to bring up the DATA 0 in the stack, assign the value to I, and then set the value to 0 again;

Therefore, best practice 1: Do not assign values to the same variable more than once in a single expression.


Java traps? Answer

1. It should be Type Mismatch
True/false is a boolean type, which is a basic data type. null generally refers to a null object, so an error is returned.
Here is an example:
Int a = null (error Reported)
Integer a = null (correct)

2. You can switch to this mode.

Fun (I = 0? Boolean. TRUE: (I = 1? Boolean. FALSE: null ))

Boolean. TRUE/Boolean. FALSE indicates the object.

3. Check the source code.
Public static final Boolean FALSE = new Boolean (false );
Public static final Boolean TRUE = new Boolean (true );

I ++ problems in JAVA?

JAVA and C ++ have different processing operations on the ++ operator. JAVA copies an I value in the memory before performing operations, while C ++ directly performs operations on the original value.
In java, the essence of the I = I ++ operation is to execute I = I first and then copy an I copy in the memory to its ++, So I is 0
In C ++, I = I is first executed. At this time, I is 0 and then I ++ is executed. At this time, I is 1, so 1 is output.

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.