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