Take a look at these lines of code. Can you determine what they do?
String firstName = "Diana";
String lastName = "Ross";
int birthYear = 1944;
System.out.println(firstName);
System.out.println(lastName);
System.out.println(firstName + lastName);
System.out.println("Disco Legend" + lastName);
System.out.println(firstName + " " + lastName + " was born in " + birthYear);
You probably figured out what that lines 1 - 3 create variables, and lines 4 and 5 print out the values stored in firstName and lastName respectfully...but what happens on line 6?
Line 6 uses the arithmetic operator (+) in a different way called concatenation. Concatenation is the process of adding Strings together. So line 6 concatenates firstName and lastName together resulting in the String "DianaRoss" being outputted to the console.
Run the code below to see it in action.
You should see the following output:
Diana
Ross
DianaRoss
Disco LegendRoss
Diana Ross was born in 1944
There is an issue with our concatenation output. We do not want to display "DianaRoss", but "Diana Ross" with a space between firstName and lastName.
To accomplish this task, concatenate a space between the variables like this: System.out.println(firstName + " " + lastName);
You can concatenate any number of Strings together using the plus sign.
Your turn: Fix the program above by correcting the spacing issues using concatenation. You can either submit a url of your forked program, or, if you fixed the code in the embedded program above, submit a screenshot.
Concatenation Rubric - 1 Point