-1
String s1="bool";
String s2="bool";
System.out.println((s1==s2)+" bool");
System.out.println(s1==s2+" bool");

Can you help me why are different output.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • 1
    Please be aware that comparing strings via `==` in Java is only deterministically safe if you want to see whether two strings are the exact same object. In rare cases, the two strings can have the same value and be different objects. None of the answers are pointing this out, but it is likely that the correct thing to do is compare via .equals(), i.e., s1.equals(s2) which checks whether the two strings have the same value, regardless of whether they are the same object. Please see http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. – DWright Apr 16 '15 at 04:07

8 Answers8

1

In the first scenario, you have 2 string compared, then " bool" appended.

In the second scenario, you have " bool" appended to s2 first.

Here it is in expanded form:

1.

boolean b = s1==s2;
String result = b + " bool";

2.

String s = s2 + " bool";
boolean result = s1 == s;

To learn more about Order of operations in Java, visit This link.

Victor2748
  • 4,149
  • 13
  • 52
  • 89
  • Why downvoting my answer? What is wrong with it? – Victor2748 Apr 16 '15 at 04:10
  • Why are people downvoting this without saying why? is it because they want their answers displayed before mine? – Victor2748 Apr 16 '15 at 04:36
  • i want to ask why its output true bool false why not it print true bool in second case . why it print only false not true – Satish Sahu Apr 16 '15 at 07:21
  • @SatishSahu Because in the second scenario, the strings are added first, then the comparison happens. So what happens is that it checks: `"bool" == "bool bool"` – Victor2748 Apr 17 '15 at 01:00
1

In first case (s1==s2) + " bool":

  • (s1 == s2) evaluates to true (since both are String literals).
  • true + "bool" this is a concatenation, where true is converted to "true" then it's appended to " bool". The result is "true bool".

In second case s1==s2+" bool":

  • + has higher priority than ==, thus s2+" bool" evaluates first to "boolbool".
  • Now, s1 == "boolbool" is evaluated which obviously evaluates to false.
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

In the first line (s1==s2)+" bool", you are explicitly comparing s1 to s2 first (by putting parenthesis around the expression) and adding bool to the result. This will compare s1 to s2, or bool == bool and then add the String bool to the resultant boolean expression.

In the second line, there are no parenthesis around the comparison expression and therefore the plus operation happens before the comparison. Based on the rules of Java, it will evaluated as if written as s1== (s2+" bool"). This will compare s2 + " bool" or bool bool, to s1, bool.

In short, the first expression will result in a String, while the second, will result in a boolean.

Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
1
System.out.println(s1==s2+" bool");    

Concatenation, +, takes precedence over comparison, ==, so concatenation evaluates first.


System.out.println((s1==s2)+" bool");

Grouping, (), takes precedence over concatenation, +, so comparison evaluates first.

joshreesjones
  • 1,934
  • 5
  • 24
  • 42
1

More visual notation is:

System.out.println((s1 == s2) + " bool");
System.out.println(s1 == (s2+" bool"));

Important note: strings must be compared with equals method:

s1.equals(s2)
Andrey Atapin
  • 7,745
  • 3
  • 28
  • 34
1

Without the parenthesis to control the order of operation, this

System.out.println(s1 == s2 + " bool");

Evaluates like

System.out.println(s1 == (s2 + " bool"));

And thus you get false. One way to mitigate this type of issue is to use formatted output. Something like

System.out.printf("%b bool%n", s1 == s2); // <-- %b is for a boolean    

Of course, it would behave the same way if s1 and s2 were primitive values.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0
(s1==s2) +" bool"

checks for s1 ansd s2 are same. If it's same or not same , it would print the result (in our case it's true) plus the string bool

s1==s2 + " bool"

check fors1 and s2+bool are same (ie, bool == bool bool). In our case, it's false.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

The reason is that operator + has precedence over operator == (check this). SO in the second case, you are first computing a string addition resulting on "bool bool" and then comparing it to S1.

Daniel Langdon
  • 5,899
  • 4
  • 28
  • 48