Review of Output and Strings

Output:

System.out and System.err are predefined objects that represent output. Use System.out to print normal program output and System.err for error messages. (We use the print or println methods of these objects.)

Exercise 1: What is displayed by the following?

   System.out.print("This");
   System.out.print(" is ");
   System.out.print("it");
Ans: Displays This is it on a single line.

Exercise 2: What is displayed by the following?

   System.out.println("This");
   System.out.println(" is ");
   System.out.println("it");
Ans: Displays
This
 is
it

Strings, string literals and concatenation

Items enclosed in double quotation marks such as "This" are called string literals. You can't split them across multiple lines.

String concatenation means joining two strings together and is represented by +. When you concatenate a string and a number, the number is converted to a string.

Exercise 3: What is the output of the following?

   System.out.println("The number is " + 24);
   System.out.println("Concatenating 24 and 45 gives " + 24 + 45);
   System.out.println("Summing first gives " + (24 + 45));
Ans:
The number is 24
Concatenating 24 and 45 gives 2445
Summing first gives 69