Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
Printing Strings and Primitive types


This exercise demonstrates how print statements execute, and it shows the precedence and purpose of the + operator with operands of different types.

Download files: PrintStatements.java
Areas: Primitive types.


PrintStatements.java
1int m = 3, n = 4;
2
3System.out.println("m: " + m);
4System.out.println(m + n);
5System.out.println("m + n: " + (m + n));
6System.out.println(m + n + (m + n));


Question 1
What does the command System.out.println do?
Solution

Question 2
What is the output of the code excerpt?
Solution

Question 3
The + operator is used in both line 3 and line 4. Is there any difference in the use of the + operator in these two lines of code?
Hints Solution

Question 4
Observe the following statement:
System.out.println("m + n: " + (m + n));
Why is the rightmost m + n enclosed in parentheses? What will the output be if the brackets are removed?
Solution

Question 5
Suppose we change the modified print statement in the previous question further by exchanging the position of the String and the numerical expression as follows:
System.out.println( m + n + "m + n: ");
What will be printed in this case?
Solution