Java Learning Note 4

Source: Internet
Author: User

1, the "+" operator in Java can not only add and subtract, but also can realize the concatenation of strings, such as somestring + someotherstring = athirdstring. Strictly speaking, the addition operator and the string concatenation operator are different, except that they use the same symbol: +. The JVM can tell in the run context whether the plus sign refers to an addition or string concatenation operator. If the JVM determines that the context is a number, it performs the addition operation, and if the JVM determines that the context is text, it performs a string concatenation operation.

int a = 1;int b = 2;int c = 3; System.out.println (A + B + c); System.out.println ("A + B + c =" + A + B + c); System.out.println ("A + B + c =" + (A + B + c));//Run result 6a + B + c = 123a + B + c = 6


The context of the first line is a number, because the first value processed by the Println method is a number. The context of the second line is text, because the first value processed by the Println method is the string literal constant. The third line of output can get the correct result because the parentheses force the addition operation to be performed, even if the context is a literal type.

2, the problem of compound assignment operation

int myInt = 2;myint *= (myInt = 4); System.out.println (MYINT);



The assignment statement in the parentheses before the print statement is myint = 4; You may think the answer must be 4. If we set the Myint to 4 and then perform the squared operation, you might think that the answer is 16. In fact, the final output of the code fragment is 8, the answer may be a bit strange, but once you figure out how the JVM handles the compound assignment operation, you'll understand. When the JVM encounters the compound assignment operator, it knows that Myint equals 2. Until the entire assignment operator is answered, the value of myint changes. By the way, this is the only one that is currently known, not the first to be processed, so the assignment in the second line of code is ignored, but the result of the assignment is used. The final result is 2 times 4, which gets 8.



Java Learning Note 4

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.