On Jul 20, 6:16 pm, Jorge Ortalli <[email protected]> wrote:
> Please, Can somebody explain further what's is happening?
> 1) System.out.println((s1==s2)+" "+ s1==s2);
> produces: false
I'll guess the explanation I've just given above
>
> 2) System.out.println((s1==s2)+" "+ (s1==s2));
> produces: true true
Here the first parenthesis is interpreted, gives true, then the second
one gives true, then the print boolean, which is interpreted as the
string true is added to a space, and again to the string true. (that's
part of the String source code which you can access from Netbeans
clicking on println).
>
> 3) System.out.println("(s1 == s2)" + " " + (s1 == s2));
> produces: (s1 == s2) true
the first part is not interpreted, its contents is purely printed as a
string, then the rest as above.
>
> 2) and 3) seem to be comparing the variables content, not the reference.
No, sorry, my bad, s1 == s2 gives the same thing in this case, so
points to the same reference. But normally, you should use equals.
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---