Showing results: print and println
A program is useful only if it can show you something. The statements System.out.print and System.out.println display information on the computer screen. They differ in one respect: what happens to the cursor, the position where the next output will appear.
System.out.println(...)displays the information and then moves the cursor to the start of a new line.System.out.print(...)displays the information and leaves the cursor where it is, so the next output continues on the same line.
public class Output {
public static void main(String[] args) {
System.out.print("One ");
System.out.print("Two");
System.out.println(" Three");
System.out.println("She said \"hello\"");
System.out.println("C:\\notes");
System.out.println("Line A\nLine B");
}
}
One Two Three
She said "hello"
C:\notes
Line A
Line B
The first three statements share one line, because the first two use print. The println in the third statement ends the line. The remaining lines show escape sequences, which we turn to next.
String literals and escape sequences
A literal is the code representation of a fixed value. A string literal is a sequence of characters enclosed in double quotes, such as "Plan first". The quotes mark where the text starts and ends; they are not part of the text displayed.
That raises a problem: how do you put a double quote inside a string, when a double quote means “end of string”? The answer is an escape sequence. An escape sequence is a special combination of characters that starts with a backslash \ and has a special meaning. The three used in this course are:
| Escape sequence | Meaning | Example string literal | Displayed as |
|---|---|---|---|
\" |
a double quote character | "say \"hi\"" |
say “hi” |
\\ |
a single backslash | "C:\\notes" |
C:\notes |
\n |
a new line | "A\nB" |
A, then B on the next line |
Each escape sequence is written with two characters but counts as one character in the string. A lone backslash followed by anything else is a syntax error, and a string whose closing quote is missing is also a syntax error.
Arithmetic expressions
An arithmetic expression is built from numeric values, variables and operators. The arithmetic operators are:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
addition | 6 + 4 |
10 |
- |
subtraction | 6 - 4 |
2 |
* |
multiplication | 6 * 4 |
24 |
/ |
division | 6 / 4 |
1 |
% |
remainder | 6 % 4 |
2 |
Two rules decide the type of the result:
- An operation on two
intvalues gives anint. - An operation with at least one
doublegives adouble.
Integer division
Look closely at 6 / 4. Both operands are int values, so the result is an int: only the integer portion of the quotient is kept. The true quotient is 1.5, the integer portion is 1, so 6 / 4 is 1. Nothing is rounded: the fractional part is thrown away. When at least one operand is a double, the result is the full quotient: 6 / 4.0 is 1.5.
The remainder operator
a % b computes the remainder when a is divided by b. For 17 % 5, 5 goes into 17 three times (15) with 2 left over, so the answer is 2. Integer division and remainder are partners: for positive numbers, a == (a / b) * b + a % b. In this course the operator is used with a not negative and b positive.
Remainders are useful: n % 10 gives the last digit of a positive whole number, n / 10 removes it, and n % 2 is 0 for even numbers.
Precedence
An expression with several operators is grouped at compile time by operator precedence:
*,/and%are evaluated before+and-.- Operators of equal precedence are evaluated from left to right.
- Parentheses override precedence: the inside is worked out first.
Running a collection of expressions makes the rules concrete:
public class Arithmetic {
public static void main(String[] args) {
System.out.println("17 / 5 = " + 17 / 5);
System.out.println("17 % 5 = " + 17 % 5);
System.out.println("17 / 5.0 = " + 17 / 5.0);
System.out.println("2 + 3 * 4 = " + (2 + 3 * 4));
System.out.println("(2 + 3) * 4 = " + (2 + 3) * 4);
System.out.println("10 - 4 - 3 = " + (10 - 4 - 3));
System.out.println("20 / 4 / 2 = " + 20 / 4 / 2);
System.out.println("20 / (4 / 2) = " + 20 / (4 / 2));
System.out.println("7 / 2 * 2.0 = " + 7 / 2 * 2.0);
System.out.println("7 / 2.0 * 2 = " + 7 / 2.0 * 2);
}
}
17 / 5 = 3
17 % 5 = 2
17 / 5.0 = 3.4
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
10 - 4 - 3 = 3
20 / 4 / 2 = 2
20 / (4 / 2) = 10
7 / 2 * 2.0 = 6.0
7 / 2.0 * 2 = 7.0
Study the last two lines. They use the same numbers, yet differ. In 7 / 2 * 2.0 the division comes first and both of its operands are int, so it gives 3; only then is 3 multiplied by 2.0. In 7 / 2.0 * 2 the first division already involves a double, so it gives 3.5, and 3.5 * 2 is 7.0. The type is decided one operation at a time, in the order of evaluation.
Dividing by zero
An attempt to divide an int by the integer zero results in an ArithmeticException, a run-time error (Topic 1.1). The same happens for % with a zero divisor. The program compiles but stops when the statement executes. (Expressions that involve a double divided by zero are outside the scope of this course.)
Worked example 1: Evaluate a mixed expression and a mixed-type expression
Task. State the value and type of (a) 10 + 7 % 4 * 2 - 9 / 2 and (b) 3 + 8 / 4 * 1.5.
Part (a), step by step. This is the expression in Figure 1.
- Identify the operators of higher precedence:
%,*and/. They are worked out before+and-. - Among
7 % 4 * 2, the operators%and*have equal precedence, so go left to right.7 % 4is 3 (7 = 1 * 4 + 3). Then3 * 2is 6. 9 / 2is integer division of twointvalues: 4.- The expression is now
10 + 6 - 4. Equal precedence, left to right:10 + 6is 16, and16 - 4is 12.
The value is 12, and its type is int, because every operation used only int values.
Part (b), step by step.
8 / 4comes first (/before+, and/is to the left of*). Both areint, so the result is theint2.2 * 1.5has adoubleoperand, so the result is thedouble3.0.3 + 3.0has adoubleoperand, so the result is thedouble6.0.
The value is 6.0, of type double.
Check. Run both: System.out.println(10 + 7 % 4 * 2 - 9 / 2); prints 12 and System.out.println(3 + 8 / 4 * 1.5); prints 6.0.
Worked example 2: Minutes into hours and minutes
Task. A program stores a total of 197 minutes in an int variable. Display it as hours and minutes, in the form 3 h 17 min. Work out the arithmetic first, then show the code and the result.
Step 1, think about what is needed. There are 60 minutes in an hour. The number of whole hours is the quotient of 197 divided by 60 with the fraction thrown away: exactly what integer division does. The minutes left over are the remainder.
Step 2, work it by hand.
- Hours:
197 / 60. 60 goes into 197 three times (180), so the integer quotient is 3. - Minutes:
197 % 60. 197 - 180 = 17, so the remainder is 17. - Check: 3 * 60 + 17 = 197.
Step 3, write the program. Printing a label, a number and another label uses + to join them (Topic 1.15 explains string joining in full). Here each + has text on one side, so it joins rather than adds.
public class Duration {
public static void main(String[] args) {
int total = 197;
int hours = total / 60;
int minutes = total % 60;
System.out.println(hours + " h " + minutes + " min");
}
}
3 h 17 min
Step 4, interpret. The output matches the hand calculation, and the check (3 * 60 + 17 = 197) shows no minutes have been lost or invented. The same pair of operators converts seconds to minutes and seconds, or splits a number of days into weeks and days. Whenever you want “how many whole groups” use /; whenever you want “how many are left over” use %.
Common misconceptions
- “
7 / 2is 3.5.” With twointoperands it is 3. Make an operand adoubleto keep the fraction. - “Integer division rounds.” It truncates:
9 / 10is 0, and19 / 10is 1. - “
%means percent.” It is the remainder operator. - “Operations are done strictly left to right.” Only operators of equal precedence go left to right.
2 + 3 * 4is 14, not 20. - “
printlnandprintdo the same thing.” Onlyprintlnends the line. - “
\nprints a backslash and an n.” It is one character, a new line. To print a backslash use\\. - “
10 - 4 - 3is 9.” Equal precedence goes left to right:(10 - 4) - 3is 3. - “If the result is going into a
double, the division is done indouble.” The division is evaluated on its own operands first.double x = 7 / 2;stores 3.0.
Where this leads
Practise on the Topic 1.3 practice questions, recap with the revision notes and tick off the checklist. The next topic, 1.4 Assignment Statements and Input, stores the results of expressions in variables and reads values typed by a user.
Quick questions
Why does 7 / 2 give 3 and not 3.5?
Both operands are int values, so Java performs integer division and keeps only the integer part of the quotient (3). To get 3.5, make at least one operand a double, as in 7 / 2.0 or 7.0 / 2.
What does the % operator do?
It gives the remainder when the first number is divided by the second. 17 % 5 is 2 because 17 is 3 groups of 5 with 2 left over. The course uses it with a non-negative first operand and a positive second operand.
What is the difference between print and println?
println displays the information and then moves the cursor to the start of a new line, so the next output appears on the next line. print displays the information and leaves the cursor where it is, so the next output continues on the same line.
How do I print a double quote inside a string?
Use the escape sequence \" inside the string literal. For example, System.out.println("She said \"hello\""); displays She said "hello".