1
String s1="hi";
String s2="hi";

boolean b1 = true;
boolean b2 = false;

(1) System.out.println(s1==s2);                            //true
(2) System.out.println(s1==s2 + s1==s2);                   //false
(3) System.out.println(s1==s2+ " " + s1==s2);              //false

(4) System.out.println(b1+b2);                             //error : bad operand types
(5) System.out.println(b1 + " " + b2);                     //true false
(6) System.out.println(true +" "+ s1==s2);                 //false
  • What is the difference between (2) & (4)?
  • What is the difference between (3) & (5)?
  • Why it gives result false in (3) & (6)?
CHAITHANYA PADALA
  • 147
  • 1
  • 1
  • 8
  • 1
    2,3 and 4 wont even compile. You cant add booleans together - it makes no sense. – pauljwilliams Mar 20 '14 at 07:33
  • 1
    **Too many questions very similar to this one** Why don't you try to read the docs and find out what operator precedence is. – Germann Arlington Mar 20 '14 at 07:43
  • only (4) compilation error. Remaining works well @PaulJWilliams – CHAITHANYA PADALA Mar 20 '14 at 07:48
  • Which language compiler you are running it in? Lines 2 and 3 **do not compile either** – Germann Arlington Mar 20 '14 at 07:56
  • Are you using Eclipse by any chance? – fge Mar 20 '14 at 08:05
  • I'm removing my answer since overall people think it's irrelevent. For reference, and since some people did upvote it anyway, I'll sum it up here. The OP did not mention explicitly that using string objects was a parameter of their problem. Thus, I believe it is relevent to state that because string comparison shouldn't be made with `==` unless comparing references, I advise as a general rule of thumb to simplify the problem by starting with experimenting with integers or using `equals`. IMHO, boolean operation precedence could have been understood more easily by the OP if they had done so. – Tonio Mar 20 '14 at 10:18

3 Answers3

12

Except for 4, all of these rely on operator precedence.

And in Java, + has precedence over ==.

Which means 2 actually "reads":

s1 == ((s2 + s1) == s2)

Therefore the right side operand of the first == is a boolean expression which compares two object references to one another (the fact that they are both Strings here is irrelevant) and here they are not the same. Hence the right side operand is boolean false.

But since the left side operand is a String, and since == is not applicable to operands String and boolean, this gives a compile error. JLS, section 15.21:

The equality operators may be used to compare two operands that are convertible (§5.1.8) to numeric type, or two operands of type boolean or Boolean, or two operands that are each of either reference type or the null type. All other cases result in a compile-time error.

If this really compiles for you, you are using a buggy Java compiler which autoboxes the right side operand to a Boolean, which it shouldn't. Let me guess: Eclipse's ECJ?

4 is an error since the + operator doesn't accept booleans as operands.

3 reads nearly the same as 2, except that this time it is s2 + " " + s1 which is (attempted to be) compared to s2. It fails to compile for the same reason.

In 5, booleans are autoboxed because of string concatenation.

6 again relies on the operator priority mentioned in 2; this time it is string true + " " + s1 which is (reference) compared with s2 (and that gives false). See 5 for what happens to true.

fge
  • 119,121
  • 33
  • 254
  • 329
  • One little comment on "6 reads the same as 2": In fact, first the exression 'true + " " + s1' is evaluated to a String, for which the literal true (which is a boolean expression) must be converted to a String first. The resulting String is then compared to s2 (with ==), which results in false. This is not really the same as 2. – Seelenvirtuose Mar 20 '14 at 07:39
  • `tring s1="hi"; String s2="hi";` s1,s2 hashCodes same **Similarly** `String sn1=new String("hai"); String sn2=new String("hai");` in this case also gives Same hashCodes sn1,sn2 **why?** – CHAITHANYA PADALA Mar 20 '14 at 07:39
  • @Seelenvirtuose I suppose here that the OP knows what string concatenation implies ;) – fge Mar 20 '14 at 07:41
  • @CHAITHANYAPADALA that is not the matter at all here; read again – fge Mar 20 '14 at 07:41
  • @fge its my another doubt [confusion] please tell me. **all of them said different hashCodes, but i got same** – CHAITHANYA PADALA Mar 20 '14 at 07:45
  • @fge I don't think so, but that doesn't matter. I wanted to correct the sentence "6 reads the same as 2", as it does not read the same. In 6, one String is assembled on the left side of the == comparison which includes type conversion, and additionally there is only one comparison. In 2, you have a trivial String assembly in the middle of the expression, and you have two comparisons, in which the second comparison again needs type conversion. As I said in my comment: It is much more complicated. – Seelenvirtuose Mar 20 '14 at 07:45
  • @CHAITHANYAPADALA I fail to see what you mean. Where on earth do you see hash codes being used at all here? – fge Mar 20 '14 at 07:46
  • @CHAITHANYA PADALA Hash codes are completely irrelevant here. – Seelenvirtuose Mar 20 '14 at 07:46
  • @CHAITHANYAPADALA are you using Eclipse by any chance? – fge Mar 20 '14 at 08:04
0
What is the difference between (2) & (4)?

Your second statement simply becomes System.out.println(hi == hihi ==hi); and the answer is false but your 4th statement is straight forward

 What is the difference between (3) & (5)?

This is same as your previous question

Why it gives result false in (3) & (6)? 

Your 3rd statement follows the above where as 6th statement convers as System.out.println("true hi" =="hi"); and output is false

P.S : '+' operator comes first in operator precedense

Siva
  • 1,938
  • 1
  • 17
  • 36
0

1) What is the difference between (2) & (4) ?

Ans :- == has more precedence over + so 2nd is actually reads as a " s1 == ((s2 + s1) == s2) " where we can't even imagine about ( boolean+boolean )

2) What is the difference between (3) & (5)?

Ans:- 3rd won't even compile and 5th is simple put values b1=true and b2=false and simple String concatenation performed

Mitul Gedeeya
  • 886
  • 1
  • 10
  • 20