17

I am facing a problem with this line (commented below):

System.out.println("Using == ::"+s3==s4)

which outputs false.

However, System.out.println(s3==s4) outputs true.

Now, I am not able to understand why I am getting this result:

public class string {
    public static void main(String[] args){
        String s3="Shantanu";
        String s4=s3;
        String s1=new String("java");
        String s2=new String("javaDeveloper");

        System.out.println("Using Equals Method::"+s1.equals(s2));
        System.out.println("Using Equals Method::"+s3.equals(s4));
        System.out.println("Using == ::"+s3==s4);//Problem is here in this line
        System.out.println(s1+"Directly printing the s2 value which is autocasted from superclass to string subclass ");
        System.out.println("Directly printing the s1 value which is autocasted from superclass to string subclass "+s2);
        System.out.println(s3);
    }
}
Output-Using Equals Method::false
Using Equals Method::true
Using == ::false
java Directly printing the s2 value which is autocasted from superclass to string subclass
Directly printing the s1 value which is autocasted from superclass to string subclass javaDeveloper
arshajii
  • 127,459
  • 24
  • 238
  • 287
Shantanu Nandan
  • 1,438
  • 8
  • 30
  • 56
  • 3
    @roippi are you a Pointy-haired Boss ? ? – jsedano Aug 14 '13 at 17:20
  • 5
    Your output is not consistent with your code. The third line should just be `false`. – arshajii Aug 14 '13 at 17:22
  • 1
    Please check that your posted output is correct. Obviously the beginning characters "Output-" wouldn't be there. Plus the last `println` call isn't shown printing `s3` and the middle line is suspect. – Lee Meador Aug 14 '13 at 17:24
  • 3
    @roippi If you hire someone because they know string concatenation has higher precedence than an equality comparison, you deserve what you get. Why not just ask those FaceBook questions, "74% of people can't get this right. What is 7 + 3 x 2 ?" – Lee Meador Aug 14 '13 at 17:30
  • Your given output is wrong. See my answer. System.out.println("Using == ::"+s3==s4); --> prints only 'false' – Christian Kuetbach Aug 14 '13 at 17:36
  • The text above the code has the right output. The 'output' section below it does not. Some people saw the above part first and some saw the below part first. All the answers saw the real issue and ignored the messy output below. – Lee Meador Aug 14 '13 at 17:36
  • 6
    @LeeMeador you're right. I'll steal that question too. Thanks. – roippi Aug 14 '13 at 17:48
  • @roippi ಠ_ಠ Those aren't good questions. – Mark Allen Aug 14 '13 at 19:39
  • 1
    Rather than using `==` you can use `.equals`. It is much more efficient than `==`. `==` fails when we are checking different types of string values. For e.g. `strObject==strVariable`. It will retun false, even if both string containing same values. – Vighanesh Gursale Aug 15 '13 at 06:40

3 Answers3

35

You're missing a set of brackets:

System.out.println("Using == ::" + (s3==s4));

In your version, "Using == ::" + s3 is being compared via == to s4, which isn't what you want.

In general, + has a higher precedence than ==, which is why "Using == ::" + s3==s4 is being evaluated as ("Using == ::" + s3) == s4.

arshajii
  • 127,459
  • 24
  • 238
  • 287
15

You are using this code:

System.out.println("Using == ::"+ s3==s4);

Which is being evaluated as:

System.out.println( ("Using == ::" + s3) == s4);

Hence you are getting false as output.

Reason is that as per the operator precedence + is higher than == as per this Operator Precedence table: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

As other answers say you need to use brackets enclosing your boolean expression:

System.out.println("Using == ::" + (s3==s4));
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    This is the best explanation of _why_ the output is what it is, but would be better with an explanation of how to fix it. – Michelle Aug 14 '13 at 17:18
  • @Michelle: IMO First I need to explain why OP's getting wrong output then my answer would be better understood. Pls check the edit now. – anubhava Aug 14 '13 at 17:19
5

The line is correct:

"Using == ::"+s3 is not equals to s4

You'll need to change your code:

"Using == ::"+(s3==s4)

edit: The output of the give code is:

Using Equals Method::false
Using Equals Method::true
false
javaDirectly printing the s2 value which is autocasted from superclass to string subclass 
Directly printing the s1 value which is autocasted from superclass to string subclass javaDeveloper
Shantanu
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79