Turn!! Analysis of Java Basic interview question: Short s1=1;s1 = S1 +1 error? Where's s1+=1?

Source: Internet
Author: User

Short s1=1;s1 = S1 +1 will you get an error?
  1. Package common;
  2. Public class Shorttypetest {
  3. /*    
  4. * @param args
  5. */
  6. public static void Main (string[] args) {
  7. //TODO auto-generated method stub
  8. Short S1 = 1, s1 = (short) (S1 + 1); Simple Type
  9. Short s2=1; s2 + =1; Composite type, compound assignment operator + =,
  10. System.out.println (s1+";")  +S2);
  11. //Runtime console output What, you know?
  12. }
  13. }

Topic:

Short S1 = 1; S1 = s1 + 1; what's wrong? Short S1 = 1; S1 + = 1; what's wrong?

Answer:
The Java specification has such rules

[

1. High turn low requires forced conversion

2. Turn the low to high position automatically.

]

Short S1 = 1; S1 = s1 + 1; what's wrong?

A: I is an int type S1 short type through the + operation after the S1 automatically converted to int type so wrong!

Short S1 = 1; S1 + = 1; what's wrong?

A: If you think that an expression (x + = i) is simply a shorthand for an expression (x = x + i), this is not accurate.

Both of these expressions are referred to as an assignment expression. (x = x + i) The expression uses a simple assignment operator (=), whereas an (x + = i) expression uses a compound assignment operator.   As mentioned in the Java language Specification, compound assignment (E1 op=e2) is equivalent to Simple assignment (e1= (t) ((E1) op (E2)), where T is the type of E1 unless E1 is computed only once. In other words, a compound assignment expression automatically transforms the result of the calculation performed to the type of the variable on its left. If the type of the result is the same as the type of the variable, then this transition has no effect. However, if the type of the result is wider than the type of the variable, then the compound assignment operator silently performs a narrowing of the native type conversion.

Therefore, compound assignment expressions can be dangerous. To avoid this unpleasant raid, do not effect a compound assignment operator on a variable of type Byte, short, or char. Because S1 is a short type, it accounts for 2 bytes, and 1 is int, which accounts for 4 bytes. When the value of two types is added, an automatic type of ascension occurs, or the data is not loaded, is this the truth *_*. In other words, after the s1+1, the result is an int, not a short type, so you can think about, put 4 bytes of things in two bytes of space, certainly compiled does not pass.

The later one does not take on the type of ascension, the Java specification says "E1+=e2 is actually e1= (T1) (E1+e2)", where T1 is the data type of E1. S1+=1 is equivalent to s1= (short) (s1+1), so it is correct

Original post address: http://blog.csdn.net/hurryjiang/article/details/9256959

Turn!! Analysis of Java Basic interview question: Short s1=1;s1 = S1 +1 error? Where's s1+=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.