问题描述:

This question already has an answer here:

  • Is it ok if I omit curly braces in Java?

    13 answers

网友答案:

In this instance, there would be no difference in performance.

However, as pointed out by @MadProgrammer, coding for readability and maintainability should always be your first priority, and optimisations for performance should only be undertaken once the code works and measurements can be taken.

Using the form with braces is good for maintainability, as it reduces the risk of the following:

if(codehere == codehere)
    doSomething();
    doSomethingElse();

When the reader skims this code, they think that both doSomething() and doSomethingElse() are only called when the if statement is true - which in this case is wrong.

If you use braces in the above example:

if(codehere == codehere) {
    doSomething();
    doSomethingElse();
}

... it becomes obvious how the code is supposed to execute.

网友答案:

No, this don't change performance. But take this link, is a good read:

http://java.dzone.com/news/omitting-braces-not-just-a-mat

相关阅读:
Top