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
weight in the assignment of message1.
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
3 and 5 is computed first, resulting in 8.
- Then this result (
8) plus "is the value " is evaluated, resulting in "8 is the value".
- Finally,
"8 is the value " plus 8 is 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 " and 3 is 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 " and 5 is 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? " and 3 is 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" and 5 is 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
3 and 5 in parenthesis is evaluated first, resulting in 8.
- The sum of
"What is the value of " and 3 is 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 " and 5 is 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? " and 8 is evaluated fifth, resulting in "What is the value of 3 plus 5? 8".
Resources
Next Learn Tutorial
Learn: Using Constants in Java