Java basic knowledge hardening 01:short s = 1; s = s + 1; with short s = 1; s + = 1;

Source: Internet
Author: User

1.short s = 1; s = s + 1; Is there a problem? What if it's solved?

Short S = 1; s + = 1; Is there a problem? What if it's solved?

2. Understanding:

Short S=1;

s=s+1;
Nature is the compiler does not pass the hint loss accuracy

So:
Short S=1;
S+=1;
Why can it be compiled through that?
There is one more question:

Implicit type conversions can be auto-byte->short->int->long from small to large, that is, if you lose precision in turn, you must perform a display type conversion
And S+=1 's meaning is different from s = s+1, s=s+1 This sentence first executes s+1 and assigns the result to S, since 1 is the int type, so the return value of the s+1 is int, and the compiler automatically performs an implicit type conversion
So assigning an int type to short will make an error, and s+=1 is different because it is the + = operator, and at parse time S+=1 is equivalent to S = (short) (s+1), which doubles as
S+=1 <=> s = (type of s) (s+1)

Java basic knowledge hardening 01:short s = 1; s = s + 1; with short s = 1; s + = 1;

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.