下面哪种格式是单行 IF 语句的 JAVA 标准? 请给我提供JAVA引用资料来支持这个论点。谢谢。
语法-01:
if (counter == 10) response.redirect("www.google.com");
语法-02:
if (counter == 10) {
response.redirect("www.google.com");
}
最佳答案
从旧的Java Code Conventions ,这是第二个。
文档的具体内容:7. 声明。 7.4 if、if-else、if-else-if-else 语句(第 12 页):
The
if-else
class of statements should have the following formif (condition) { statements; } if (condition) { statements; } else { statements; } if (condition) { statements; } else if (condition) { statements; } else if (condition) { statements; }
Note:
if
statements always use braces{}
. Avoid the following error-prone form:if (condition) //AVOID! THIS OMITS THE BRACES {}! statement;
此后,由于 Java 代码约定确实很旧(自 1997 年以来),由于代码可读性,这属于个人问题/品味。 IMO 第二个就很好了。
关于java - 单行 if 语句的 Java 标准是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24729206/