Java字符串初始化
您更喜欢哪个以及为什么”
String myString = null;
if(someCondition)
myString = "something";
else
myString = "something else";
或者
String myString = "";
if(someCondition)
myString = "something";
else
myString = "something else";
我知道可以使用三元 (?:) 运算符,但我想了解以上两个。
Which do you prefer and why"
String myString = null;
if(someCondition)
myString = "something";
else
myString = "something else";
OR
String myString = "";
if(someCondition)
myString = "something";
else
myString = "something else";
I know that using the ternary (? :) operator is possible but I'd like to know about the above two.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
惯用的方法是使用三元/条件运算符 (JLS 15.25 ):
但是如果您确实觉得必须这样做,您也可以执行更详细的
if-else
语句:请注意,我在上面添加了
final
修饰符片段。如果您计划进一步重新分配该变量,那么它当然不能是final
,因此您可以删除修饰符,当然代码仍然可以工作。为什么是
最终
?上面代码片段中
final
的要点是表明if-else
构造将在所有可能的情况下分配给myString
一次且恰好一次执行路径。这是所提议的if-else
解决方案的主要思想:如果您只想为局部变量赋值一次,即使它可能是多种可能性之一,那么请将其设为< code>final 以增强可读性。与此“替代”提案进行对比:
使用此构造,您可能会分配给
myString
两次,因此即使没有,您也不能将final
放在这里进一步重新分配。您也不能将final
放入原始= null;
或= "";
提案中,这是主要的之一不推荐它们的原因。如果您只是想在使用变量之前覆盖它,那么为变量赋值是没有意义的。它会损害可读性,甚至可能隐藏错误,例如,当一个执行路径无法覆盖此“初始”值时。
参考文献
final 变量
总结
final
以增强可读性final
立即向读者保证,不可能进一步重新分配The idiomatic way is to use ternary/conditional operator (JLS 15.25):
But you can also do the more verbose
if-else
statement if you really feel you must:Note that I've added
final
modifier in the above snippet. If you're planning on further reassignments to the variable, then of course it can't befinal
, so you can remove the modifier and of course the code would still work.Why
final
?The point of the
final
in the above snippet is to show that theif-else
construct will assign tomyString
once and exactly once in all possible execution paths. That is the main idea of the proposedif-else
solution: if you're going to assign a value to a local variable only once, even if it can be one of several possibilities, then make itfinal
to enhance readability.Contrast that with this "alternative" proposal for example:
With this construct, you may be assigning to
myString
twice, thus you couldn't putfinal
here even if there was no further reassignment. You also couldn't putfinal
in either of the original= null;
or= "";
proposals, and this is one of the main reasons why they're not recommendable.There's no point in assigning a value to a variable if you're just going to overwrite it before you're going to use it. It hurts readability, and may potentially even hide bugs, e.g. when one execution path fails to overwrite this "initial" value.
References
final
VariablesSummary
final
to enhance readabilityfinal
immediately assures readers that no further reassignments are possible初始化步骤不是必需的,并且可能会使未来的读者感到困惑。
我个人的观点是,这种变量只能分配一次,因此它是
final
关键字的完美候选者。请注意,myString 定义不包含赋值(因为这会禁止以后的赋值),并且赋值后它是只读的。这提供了健壮的代码并更清楚地显示您的意图。
另请注意,即使对于单行,我也相信大括号。可能是 Perl 的习惯,但如果你不这样做,总有一天它会咬你一口。
The initialization step is not necessary, and may confuse future readers.
My personal opinion is that this kind of variable should only be assigned once, hence it is a perfect candidate for the
final
keyword.Note that the myString definition does not include an assignment (as this would prohibit later assignments) and that after the assignment it is read-only. This gives robust code and shows your intent more clearly.
Please also note that I believe in braces even for single lines. Probably a Perl habit, but if you don't, it will bite you someday.
我更喜欢第一个,因为
String myString = ""
将在池中创建其他对象I prefer first one, because
String myString = ""
will create additional object in the pool上面会因空指针而导致错误。
我喜欢稍后使用,但我认为这是个人喜好。
Above will cause error due to null pointer.
I like to use later, but I think it's personal preference.
下面的代码怎么样,无论如何他想设置一些东西。
或者
在上面的情况下,如果您 90% 确定 someCondition 始终为 true。否则在声明中创建不必要的对象。期待专家的评论。
How about this follwing code ,anyways he wants to set something.
or this
in the above case , if you are 90% sure that someCondition is always true. otherwise unnecessary object creation in declaration.Expecting comments from Gurus.
两者都不。相反,这样:
在您的两种选择中,变量都使用永远不会被读取的值进行初始化。它的存在这一事实具有误导性。
当然,我实际上会使用条件运算符 - 但除此之外,以上是更好的选择。
Neither. Instead, this:
In both of your alternatives, the variable is initialized with a value which will never ever be read. The fact that it's present at all is misleading.
I would actually use the conditional operator, of course - but barring that, the above is the better option.