课程: Learning Java 17

Concatenating strings - Java教程

课程: Learning Java 17

Concatenating strings

- [Instructor] So far, we've printed out a few strings individually but now we're going to print them together on the same line. Using the example from the last lesson, we could print out a student's full name and say the student has a GPA of X, with X being the student's current GPA. In the code, we'll write out system.out.println in as usual. Then, we'll reference the student first name variable. To add on the last name, we'll use the plus sign to combine the value of these two strengths. This is called concatenation and we refer to the plus sign as the concatenation operator. Now, since we're printing these variables one after the other, there will be no space between them when they're displayed to the user. We can add a space by adding a literal string and it'll contain a space. In addition to the name, we'll also want to add on, has a GPA of, and then the student's GPA. We'll tack on the string, has a GPA of, and then concatenate the variable studentGPA. Let's run it. In the console there's a statement with the student's name and GPA.

内容