I just was tidying my code a bit and there was this piece:
String saving = getValue();
if(saving != null && saving.equals("true")){
// do something
}
Then I thought of doing it the other way around to get rid of the checking for null
:
if("true".equals(saving)){
// do something
}
It definitely works, but is this safe to do so? I mean string literals are stored in a common pool, while string object create by new
are on the heap. But strings in the constant pool are also objects, right?
But still it doesn't seem like the right thing to do, even though it makes the code shorter.
See Question&Answers more detail:os