• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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
 
Maybe he went home and went to bed. And took this tiny ad with him:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic