It depends. If I'm interested in whether s is null or whether it's equal to the string literal, then I'd go for a seperate null check and the literal on the right. If I don't care if s is null, all I'm interested in is whether it equals the literal, then string literal on the right. On a purely personal note, I prefer the literal on the right but that's just habit.
Joined: Jul 17, 2006
Personally, I'm more in favor of variable.equals("literal"), because it seems more logical to me when I read it.
The intent is rarely to see if two strings are the same, but rather, to see if the variable holds some specific significant value (if that makes sense to any of you). To do that, we check to see if they are the same.
But to me, when I see "literal".equals(value), I think to myself, why am I checking the value of "literal". I know its value. My intention is to check the value of variable. So I prefer to see variable to the left, because it reads as 'if variable is equal to "literal"', which is what I want.
Having said that, "literal".equals(value) means we don't have to null check. Some people think this is a good thing. Personally, I'd rather see the null check, because it tells me that it is possible that this variable is null. If I don't see the null check, I don't know if there's a possibility that the variable is null.