-3

My code below

public class EqualsComMetod_Operator {

  public static void main(String[] args) {

    String s1=new String("Raja");
    String s2=new String("Raja");
    System.out.println("s1==s2::"+s1==s2);
    // Here the text s1==s2:: not printed on my console why??
  }
}

Output:

false

Am comparing the objects as reference/address and try to print like this:

s1==s2::false

but directly showing false. Why?

Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • try `System.out.println("s1==s2::"+(s1==s2));` after enclosing it inside the parenthesis. – Braj Sep 07 '14 at 06:11
  • "s1==s2::"+s1==s2 will be interpreted as "s1==s2::"+s1 == s2 which is not true. Thats why you got false. Try this "s1==s2::"+(s1==s2) – prasanth Sep 07 '14 at 06:12
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – SparkOn Sep 07 '14 at 06:37
  • @SparkOn It's a different problem. OP is not understanding order of operations in this case. As he pointed out, he _knows_ the output of operation `==` is `false`. – Unihedron Sep 07 '14 at 06:39
  • @SparkOn, no, that is not a dup. This is about `+` binding more tightly than `==`. – Bart Kiers Sep 07 '14 at 06:39
  • @Unihedron i don't find any different context in here – SparkOn Sep 07 '14 at 06:40
  • 1
    @SparkOn "Output: `false` Expected output: `s1==s2::false`" – Unihedron Sep 07 '14 at 06:40
  • possible duplicate of [Java operator precedence guidelines](http://stackoverflow.com/questions/2137690/java-operator-precedence-guidelines) – Unihedron Sep 07 '14 at 06:51

3 Answers3

1

Statement should be

System.out.println("s1==s2::" + (s1 == s2));
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Nagesh Kumar
  • 293
  • 1
  • 10
  • Answer may be posted while someone might be typing doesn't means the answer is copied. There could be only one way of doing things in right way and same answer can be posted by others. – Nagesh Kumar Sep 07 '14 at 06:55
  • 1
    http://meta.stackoverflow.com/questions/267090/why-are-users-answering-questions-as-quickly-as-possible/267105#267105 – Unihedron Sep 07 '14 at 06:56
1

The operator + is evaluated before the ==.

So if you examine the expression "s1==s2::"+s1==s2:

First "s1==s2::"+s1 is evaluated. It results in "s1==s2::Raja". Then, "s1==s2::Raja"==s2 is evaluated, and obviously results in false.

You can control the precedence using brackets:

public static void main(String[] args) {
    String s1 = new String("Raja");
    String s2 = new String("Raja");
    System.out.println("s1==s2::" + (s1 == s2));
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Mureinik
  • 297,002
  • 52
  • 306
  • 350
-3

When you create two strings using new, they are different objects. The equality operator will always return false even if the strings contain the same content. This differs from literal strings, which the compiler will optimize to refer to the same object.

String s1 = new String("Raja");
String s2 = new String("Raja");
System.out.println(s1 == s2); // false

String s3 = "Raja";
String s4 = "Raja";
System.out.println(s3 == s4); // true
Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82