• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

boolean and String concatenation

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10811
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1205
22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes! Many thanks to both of you.
reply
    Bookmark Topic Watch Topic
  • New Topic