Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

boolean and String concatenation

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can anyone please explain why with == operator in line 9 prints false whereas in line 10 prints true.
Thanks!
 
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are comparing the references to "ab" and "ab " (note the extra space).

The '+' operator has a higher precedence than the '==' operator.
 
Ranch Hand
Posts: 1170
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You are comparing the references to "ab" and "ab " (note the extra space).

The '+' operator has a higher precedence than the '==' operator.



Exactly.  Line 9 is equivalent to...


...which, as Carey pointed out, is comparing "ab" to "ab<space>"

You probably want to change that to...



Similarly, line 11 is equivalent to...

...in which the == is comparing "true ab" to "ab".
 
Neha Agnihotri
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes! Many thanks to both of you.
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!