Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
Java does not execute a file one source line at a time. It executes complete statements. In this program, the two calls to System.out.println are the only executable statements, so the output is Hello followed by Java.
class Main {
public static void main(String[] args) {
// Print the first word.
System.out.println(
"Hello"
);
/* The next statement fits on one line. */
System.out.println("Java");
// The final statement.
}
}The class declaration and method declaration describe where the code belongs, but neither one is an action that prints text. The braces group the code inside those declarations. The three comments describe the code for a reader and are ignored when Java executes it. None of those parts increases the count of executable statements.
How many executable statements does the program contain, and which source lines belong to the first one?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Trace the first call from the method name System.out.println through its opening parenthesis, the string "Hello", its closing parenthesis, and the semicolon. The semicolon after the closing parenthesis ends the complete statement. The line breaks inside that path do not end it, so Java reads all three source lines as one method-call statement.
System.out.println(
"Hello"
);A semicolon is not a replacement for every missing piece of syntax. Adding one after the comment line does not make the comment executable, and adding one after a closing brace does not turn that brace into a method-call statement. The semicolon works here because it follows a complete method call.
The line comment // Print the first word. starts at its two slash characters and continues to the end of that source line. Java ignores the comment text, then reads the println statement on the following lines. The comment does not need a semicolon because it is not a statement.
The block comment /* The next statement fits on one line. */ starts at /* and ends at */. It occupies part of one source line here, but a block comment can also cross source line boundaries. Java removes the comment from the code it executes, so changing only the words inside either comment does not change the two println calls or their output.
// Print the first word.
System.out.println(
"Hello"
);
/* The next statement fits on one line. */
System.out.println("Java");
// The final statement.The closing block-comment delimiter is missing below. Type the exact two characters that restore the comment before the second println statement.
class Main {
public static void main(String[] args) {
// Print the first word.
System.out.println(
"Hello"
);
/* The next statement fits on one line.
System.out.println("Java");
// The final statement.
}
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The two characters // activate a line comment from their position to the end of that source line. The pair /* opens a block comment, and */ closes it at the next matching endpoint. Java does not guess whether text looks like code: if it lies between those delimiters, Java treats it as comment text.
class Main {
public static void main(String[] args) {
// Print the first word.
System.out.println(
"Hello"
);
/* The next statement fits on one line.
System.out.println("Java"); */
// The final statement.
}
}With the closing */ moved past System.out.println("Java");, the second call is no longer active code. It is part of the block comment, along with the words before it. The first println call remains outside the comment, so the program prints only Hello. Moving just one comment delimiter changes which statement Java can execute.