Java Reference
In-Depth Information
}
JoinStrings.java
If you run this example, it produces some interesting results:
Many hands make light work
99 hands make light work
fifty-five is 55
10 is ten
How It Works
The first statement after defining the variables is:
myString = firstString + secondString + thirdString;
This joins the three string values stored in the
String
variables —
firstString
,
secondString,
and
thirdString
— into a single string and stores this in the variable
myString
. This is then used in the
next statement to present the first line of output.
The next statement that produces a new string uses the
+
operator you have used regularly with the
println()
method to combine strings, but clearly something a little more complicated is happening
here:
myString = numHands + " " + secondString + thirdString;
This operation is illustrated in
Figure 4-8
.