Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
The three output calls in the program run from top to bottom. The first call writes Score: , the second writes 7, and the third writes Ready. The console receives those calls in that order, regardless of the spaces or line breaks used to arrange the source code.
class Main {
public static void main(String[] args) {
System.out.print("Score: ");
System.out.println(7);
System.out.println("Ready");
}
}A new source line after an output call does not create a new console line by itself. The first call can leave the output position on the same line, allowing the next call to continue there. The visible result is Score: 7 on the first console line and Ready on the second.
System.out.print("Score: "); writes the characters S, c, o, r, e, :, and the space inside the string literal. The console now contains Score: , with the output position immediately after that trailing space. The line is still unfinished, so the next output call begins at that position.
Where will 7 appear after System.out.print("Score: "); runs in the running program?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
System.out.println(7); starts at the position left by print, so it appends 7 after Score: and its trailing space. It then moves the output position to the start of the next console line. System.out.println("Ready"); writes Ready on that second line and moves the position again, this time to the start of a third line.
The first call was mistakenly written with println. Replace only the method name so the exact first line becomes Score: 7.
System.out.println("Score: ");Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The space between the colon and 7 comes from the final character inside the string literal "Score: ". Removing that character produces Score:7, because Java does not insert a separating space automatically. The line break after 7 and the line break after Ready come from the two println calls, not from the source lines that contain them.
System.out.print("Score: ");
System.out.println(7);
System.out.println("Ready");If the first call is changed to System.out.println("Score: ");, it prints Score: and then finishes the first line. The next println writes 7 on the second line, and the final println writes Ready on the third line. Changing one method name therefore changes the visible line structure, even though the text being printed stays the same.