In java "string" == "string" is false why? -
the following statement appears in section 3.10.5. string literals of java language specification:
a string literal refers same instance of class string. because string literals - or, more generally, strings values of constant expressions (§15.28) - "interned" share unique instances, using method string.intern.
i using java jdk 7 , eclipse indigo.
and test program follows:
public class main { public static void main(string[] args) { string s1 = "string"; string s2 = "string"; system.out.print(s1 == s2); // true system.out.print(" , " + "string" == "string"); // false } }
this operator precedence issue. ==
operator has lower precedence +
operator.
what testing whether (" , " + "string")
equal "string"
. isn't.
if mean compare "string"
, "string"
should write:
system.out.print(" , " + ("string" == "string"));
the standard advice of not using ==
test strings applies ... that's not giving confusing output.
Comments
Post a Comment