Creating a New String by Joining Two Strings
The addition operator can be used to append, or join, two String objects together or append a String object with a primitive value. In order to append a String with a primitive, the compiler will automatically create a String literal out of the primitive value. This is a String Conversion.
It is important to remember that items are added together from left to right. This applies to expressions that involve String literals as well.
Your Turn
Example 1
In all the cases above, we could have assigned the resulting String literal to the variable.
Example 2
Where this is really powerful, is if the primitive values are being stored in a variable of their own.
Example 3
- Modify the code below to use the variable
weightin the assignment ofmessage1.
Order of Operations with String Literals and Primitives
- Predict the value that will be assigned to
sumMessage. - Check your thinking by running it in the Java Playground.
Your Turn
Visual Illustration
This visual illustrates the order of operations for this assignment statement.
Explanation:
- The sum of
3and5is computed first, resulting in8. - Then this result (
8) plus"is the value "is evaluated, resulting in"8 is the value". - Finally,
"8 is the value "plus8is evalued, resulting in"8 is the value 8".
Your Turn
Let's try this in the Java Playground.
- Predict the value that will be assigned to
sumMessage2. - Check your thinking by running it in the Java Playground.
Explanation:
- The sum of
"What is the value of "and3is evaluated first, resulting in"What is the value of 3". - The sum of
"What is the value of 3"and" plus "is evaluted second, resulting in"What is the value of 3 plus ". - The sum of
"What is the value of 3 plus "and5is evaluated third, resulting in"What is the value of 3 plus 5". - The sum of
"What is the value of 3 plus 5"and?is evaluated forth, resulting in"What is the value of 3 plus 5? ". - The sum of
"What is the value of 3 plus 5? "and3is evaluated fifth, resulting in"What is the value of 3 plus 5? 3". - The sum of
"What is the value of 3 plus 5? 3"and5is evaluated sixth, resulting in"What is the value of 3 plus 5? 35".
You can use parenthesis to override the order of operation so to correct this output to be "What is the value of 3 plus 5? 8".
Your Turn
Let's try this in the Java Playground.
- Predict the value that will be assigned to
sumMessage3. - Check your thinking by running it in the Java Playground.
Explanation:
- The sum of
3and5in parenthesis is evaluated first, resulting in8. - The sum of
"What is the value of "and3is evaluated second, resulting in"What is the value of 3". - The sum of
"What is the value of 3"and" plus "is evaluted third, resulting in"What is the value of 3 plus ". - The sum of
"What is the value of 3 plus "and5is evaluated forth, resulting in"What is the value of 3 plus 5". - The sum of
"What is the value of 3 plus 5"and?is evaluated fifth, resulting in"What is the value of 3 plus 5? ". - The sum of
"What is the value of 3 plus 5? "and8is evaluated fifth, resulting in"What is the value of 3 plus 5? 8".