Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
int x = 5;
if (x > 10)
System.out.print("A");
System.out.print("B");
}
}B
- Expecting nothing: indentation does not group statements in Java; without braces only
print("A")belongs to theif, andprint("B")always runs. - Expecting a compile error: braces are optional for a one-statement branch, so the code compiles and only the indentation misleads.
- How would you change the code so that neither letter is printed?
- Why do style guides require braces even around a one-line branch?