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.
In java, a String is initialized in multiple ways. Either by using constructor or by using Literal. There is a difference in initializing String by using a new keyword & using Literal. Initializing String using new keywords every time create a new java object.
There are two ways to create a String object: By string literal : Java String literal is created by using double quotes. For Example: String s=“Welcome”; By new keyword : Java String is created by using a keyword “new”.
Initialize a string by passing a literal, quoted character array as an argument to the constructor. Initialize a string using the equal sign (=). Use one string to initialize another. These are the simplest forms of string initialization, but variations offer more flexibility and control.
Neither. Instead, this:
String myString;
if (someCondition)
myString = "something";
else
myString = "something else";
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.
The idiomatic way is to use ternary/conditional operator (JLS 15.25):
String myString = (someCondition ? "something" : "something else");
But you can also do the more verbose if-else
statement if you really feel you must:
final String myString;
if(someCondition) {
myString = "something";
} else {
myString = "something else";
}
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 be final
, so you can remove the modifier and of course the code would still work.
final
?The point of the final
in the above snippet is to show that the if-else
construct will assign to myString
once and exactly once in all possible execution paths. That is the main idea of the proposed if-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 it final
to enhance readability.
Contrast that with this "alternative" proposal for example:
// DON'T DO THIS! Example only!
String myString = "something else";
if (someCondition) myString = "something";
With this construct, you may be assigning to myString
twice, thus you couldn't put final
here even if there was no further reassignment. You also couldn't put final
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.
final
Variablesfinal
to enhance readability
final
immediately assures readers that no further reassignments are possibleThe 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.
final String myString;
if (someCondition) {
myString = "something";
} else {
myString = "something else";
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With