This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Different output without the parantheses

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer the below two programs:

1)


Output:



2)



output




For the second program I get the output as expected. Here b==c is true

wherease for the first program, I have not given the parantheses, the results vary. I am not getting whatever I given in the double quotes. Also I expect b==c to be true, but it prints as false.

Please help me to understand how the parantheses play the role to get different outputs here.
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Loganathan...Please use code tags in other to make your programs easier to read.


Output


Output
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Loganathan, the pritnln statement in your first program is carrying out string concatenation (adding "b==c" to the value of 'b' which evaluates to "b==cabc" and comparing the result to 'c' ) that is why the output is different, with this:

The output will be as expected:

Always watchout for 'String Concatenation'.

Regards

Ikpefua.
 
Loganathan Karunakaran
Ranch Hand
Posts: 79
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ikpefua,

Thanks for your time and explanation. I understood the difference. Here the operator precedence comes into the rule without the parantheses. The + operator has higher precedence than the == operator. Thanks a lot.

Regards
Loganathan
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic