- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在观看 newbostons 的基本 java 教程时,他教我们做这样的 if 语句。(注意花括号)
if("pie"== "pie"){
System.out.println("Hurrah!");
}
所以我试着去掉花括号
if("pie"== "pie")
System.out.println("Hurrah!");
它仍然有效!由于我是java新手,我不知道为什么会这样。而且我想知道删除(或添加)花括号是否有任何好处/坏处。
最佳答案
对于单个语句,它将保持不变,但如果您想在 if block 中组合多个语句,则必须使用花括号。
if("pie"== "pie"){
System.out.println("Hurrah!");
System.out.println("Hurrah!2");
}
if("pie"== "pie")
System.out.println("Hurrah!"); //without braces only this statement will fall under if
System.out.println("Hurrah!2"); //not this one
您应该看到:Blocks in Java
A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. The following example, BlockDemo, illustrates the use of blocks:
class BlockDemo {
public static void main(String[] args) {
boolean condition = true;
if (condition) { // begin block 1
System.out.println("Condition is true.");
} // end block one
else { // begin block 2
System.out.println("Condition is false.");
} // end block 2
}
}
(来自上述链接的示例)
关于java - 从 java 中的 If 语句中删除花括号是否有区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15786949/
我用 redis 后端运行 celery。 我想在 centos 6.2 上运行 celery 花作为守护进程。 我知道花是 Tornado 应用程序,所以我应该使用一个进程来运行 Tornado 应
我在一台服务器上有几个项目,它们使用具有不同 BROKER_URL 的 celery 包。Flower 允许一个 BORKER_URL 作为命令选项: celery flower --broker=a
我正在使用芹菜和花卉。当我访问Flower中的“任务”标签时,我可以看到我的任务正在注册,甚至可以在“状态”列中看到“成功”标签以及所有内容。 但是,在“监视器”选项卡上,所有图形(“成功任务”,“失
我正在尝试创建一个有 5 个叶子的 css 花 - 没有任何有希望的尝试。我正在尝试创建什么:有 5 片叶子的 CSS 花,每片叶子可以有三种不同的状态(小、中、大)。我找到了 http://ross
如果我有一个变体,像这样: using my_variant = boost::variant; 有没有一种简单的方法可以将变体可以包含的类型提取到 Boost.Hana 元组中,以便满足以下条件:
调用 Flower API's /api/task/apply/* 只是挂起而没有返回结果,即使任务已根据 Web UI 处理并成功。 重现错误: # hangs even though task s
我有一个模板函数,默认情况下没有定义,但它由某些类型特化: template auto foo(bar &, const T &) -> void; template <> auto foo(bar
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我是一名优秀的程序员,十分优秀!