Java 一行 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20116639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:14:35  来源:igfitidea点击:

Java one line if statement

java

提问by Subodh Joshi

I am using if condition without braces in java something like

我在java中使用if条件没有大括号类似

if(somecondition)
//Only one line Business logic

but some told use braces always even one line statement something like this

但有些人告诉使用大括号总是甚至像这样的一行语句

if(somecondition){
//Only one line Business logic 
}

What is the better way according to java sandard?

根据java sandard,更好的方法是什么?

回答by radai

there's no real "standard". i prefer always using braces because if you dont you risk someone adding an innocent looking logging statement turning your code from

没有真正的“标准”。我更喜欢总是使用大括号,因为如果你不这样做,你就会冒险添加一个看起来很无辜的日志语句来将你的代码从

if(somecondition)
//Only one line Business logic

into

进入

if(somecondition)
log.debug("condition was true");
//Only one line Business logic

and then things stop working :-)

然后事情停止工作:-)

回答by shreyansh jogi

According to java standard braces are better because if they are not there compiler has to work around more and also would be performance issue.

根据java标准大括号更好,因为如果没有它们,编译器必须解决更多问题,而且还会出现性能问题。

回答by Masudul

Using if statement with braces is better way to java standard, because it increase the readability and reduce unwanted error.

使用带大括号的 if 语句是 Java 标准的更好方法,因为它增加了可读性并减少了不必要的错误。

回答by Ivaylo Strandjev

The two statements have exactly the same effect but I have suffered so often from the lack of braces that I also always comment that there should be braces even around 1 line statements. This makes the code easier to maintain and can save a lot of headache. My experience shows that one line if statements often turn into multi-line statements on later iterations so what you save by not writing two {the first time, you will give later on.

这两个语句具有完全相同的效果,但我经常因缺少大括号而受苦,以至于我也总是评论说即使在 1 行语句附近也应该有大括号。这使得代码更易于维护,并且可以省去很多麻烦。我的经验表明,一行 if 语句在以后的迭代中经常会变成多行语句,所以你{第一次不写两行所节省的,你会在以后给出。

回答by Shaun

No naked if statements. You're just asking for trouble. Always use { }

没有赤裸裸的 if 语句。你只是自找麻烦。始终使用 { }

回答by RamonBoza

it is better to use braces when checking for errors or updating the code.

检查错误或更新代码时最好使用大括号。

imagine.

想象。

if(answer.equals("add"))
    addedValue += Scanner.readInt();

but you have a new requirement to add only the absolute value, so you change to.

但是您有一个新要求,只能添加绝对值,因此您更改为。

if(answer.equals("add2))
    valueToBeAdded = Scanner.readInt();
    if(valueToBeAdded < 0) valueToBeAdded = - valueToBeAdded;
    addedValue += valueToBeAdded;

this is not a really correct algorithm, is just an example of what can happens.

这不是一个真正正确的算法,只是可能发生的一个例子。

回答by user2314737

That's a matter of taste. I would use braces or else no braces but write all code in one line to improve readability.

那是品味问题。我会使用大括号或不使用大括号,而是将所有代码写在一行中以提高可读性。

Also you might consider using a ternary operator

您也可以考虑使用三元运算符

booleanExpression ? value1 : value2

回答by benzonico

In addition to @radai answer, if you are a real evil mind, when you see a if with no braces you can do something that will make you ennemies by adding a semi-colon on the same line of the if but at the 800th column of the line(or something).

除了@radai 的回答,如果你是一个真正邪恶的人,当你看到一个没有大括号的 if 时,你可以通过在 if 的同一行但在第 800 列添加一个分号来做一些让你成为敌人的事情行(或某事)。

like

喜欢

if(condition) /*a loooot of whitespace*/ ;
    //Only one line Business logic that will get executed whatever is the condition

This is why i prefer to use braces and recommend people to use them

这就是为什么我更喜欢使用牙套并推荐人们使用它们