Question

Java allows for certain keywords to be followed by a statement or a statement block. For example:

if (true)
    System.out.println("true");

do
    System.out.println("true");
while (true);

compiles as well as

if(true) {
    System.out.println("true");
}

do {
   System.out.println("true");
} while (true);

This is also true for keywords like for, while etc.

However, some keywords don't allow this. synchronized requires a block statement. Same for try ... catch ... finally, which requires at least two block statements following the keywords. For example:

try {
    System.out.println("try");
} finally {
    System.out.println("finally");
}

synchronized(this) {
    System.out.println("synchronized");
}

works, but the following doesn't compile:

try
    System.out.println("try");
finally
    System.out.println("finally");

synchronized (this)
    System.out.println("synchronized");

So why do some keywords in Java require a block statement, while others allow a block statement as well as a single statement? Is this an inconsistency in language design, or is there a certain reason for this?

Was it helpful?

Solution

You get a dangling else-like ambiguity if you try to allow leaving out the braces. Whilst this could be solved in a similar fashion to the dangling-else, probably best not to.

Consider

try
try 
fn();
catch (GException exc)
g();
catch (HException exc)
h();
catch (IException exc)
i();

Does that mean

try
    try 
        fn();
    catch (GException exc)
        g();
    catch (HException exc)
        h();
catch (IException exc)
    i();

or

try
    try 
        fn();
    catch (GException exc)
        g();
catch (HException exc)
    h();
catch (IException exc)
    i();

I believe in CLU, catch blocks were around just one statement (may be wrong).

OTHER TIPS

It's just the design decision of the language, and its compiler mechanics.

I agree with the decision. Not requiring a code block might make the code shorter, but it's a sure fire way to cause confusion and create unforeseen consequences.

There are problems with not using { } even with statements which allow this there can be confusion. The way a deal with this is to rigorously use code formatters. Many places require { } always to avoid problems.

e.g.

if (condition)
    if (condition2)
        statement
  else // which if
     statement

do
    statement
    while (condition) // is it do/while or an inner loop?
       statement
  while (condition2)
    statement

I believe the reason you can do this for some statements and not others from from C. In C you can use if/do/while/for without a statement block. However try/catch and synchronized have been added in Java. There are two reasons why these only have { } blocks.

  • it was considered best practice
  • it is simpler to allow only one option.

Given Java is a feature lean language, I suspect it is the later as much or more than the former.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top