Joining strings with +
Between pieces of text, + joins them into one longer text. This is called concatenation. It works with variables too, so a program can build a sentence around what the user typed:
Scanner scanner = new Scanner(System.in);
System.out.println("What is your name?");
String name = scanner.nextLine();
System.out.println("Hi " + name + ", nice to meet you!");
AdaWhat is your name?
Hi Ada, nice to meet you!(From here on, some examples show only the lines inside main. The class, the main method and the import around them are the same as before.)
+ adds no spaces of its own. Every space in the result has to be inside some quotes: "Hi " + name gives Hi Ada, but "Hi" + name gives HiAda. The same goes for commas and other punctuation.
A single println can join as many pieces as you like: "a" + b + "c" + d works from left to right.