4. String Concatenation¶
4.1. Objectives¶
- Describe what is meant by concatenation
- Utilize the
+
operator to properly concatenate strings in Java
4.2. Time Goal¶
- 5 minutes on this section
4.3. Key Terms¶
- append
- One string can be appended to another using the + operator. This will create a new string with all the characters in the first string followed by all the characters in the second string.
- concatenate
- One string can be concatenated after another which is the same as appending one string after another.
- immutable
- Means that something doesn’t change. Strings are immutable. Any method that seems to change a string actually creates a new string.
4.4. Exercises¶
Note
Strings can be appended to each other which creates a new string using the +
operator . This is also called concatenation.
Note
Note that spaces are not added between strings automatically. If you want a space between two strings then add one.
Note
You can even add other items to a string using the +
operator. The other item will be converted to a string using the toString
operator if it is an object and then appended to the current string. All objects inherit a toString
method that returns a string representation of the object.
What do you think the following will print?
Note
If you are appending a number to a string it will be converted to a string first before being appended.
Since the same operators are processed from left to right this will print 1243
. First 4 will be turned into a string and appended to 12 and then 3 will be turned into a string and appended to 124. If you want the addition to take place before the numbers are turned into a string what should you do? Try to modify the code above so that it adds 4 + 3 before appending the value to the string.
- xyz
- s1 will equal "xy" plus another "xy" then z at the end.
- xyxyz
- s1 contains the original value, plus itself, plus "z"
- xy xy z
- No spaces are added during concatenation.
- xy z
- No spaces are added during concatenation, and an additional "xy" should be included at the beginning.
- z
- s1 was set to "xy" initially, so the final answer will be "xyxyz"
Q-124: Given the following code segment, what is in the string referenced by s1?
String s1 = "xy";
String s2 = s1;
s1 = s1 + s2 + "z";
- 21
- This would be correct if it was System.out.println(13 + 5 + 3), but the 13 is a string.
- 1353
- This is string concatenation. When you apprend a number to a string it get turned into a string and processing is from left to right.
- It will give a run-time error
- You can append a number to a string in Java. It turns the number into a string and then appends the second string to the first string.
- 138
- This would be correct if it was System.out.println("13" + (5 + 3)), but the 5 is turned into a string and appended to the 13 and then the same is done with the 3.
- It will give a compile-time error
- You can append a number to a string in Java. It will compile.
Q-125: What does the following code print?
System.out.println("13" + 5 + 3);