• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

EqualsEx

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


OUTPUT : 1 false 2 true false 5 false 6 false

My Doubt is why the string " 3 " ," 4 " in line 1 are not printing and also totally we have 6 boolean tests but why I am getting only 5 boolean literals.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First i was surprised to see the result .... then i felt funny ..... it happens
check the brackets in the LINE one

 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will work fine if you change "Line 1" to

System.out.print(" 3 " + (s2 == s3) + " 4 " + (s2 == s4));//Line 1

Dawn.
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya I replaced . Now I am able to get the Strings " 3" and " 4 " in the output. But still i didn't get the logic in the brackets(). What happens if there is no bracket ???
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
" 3 " + (s2 == s3) + " 4 " + s2
this part of the statement results to
"sfalse4java"----------- which is a string.
Now you are comparing this... it would be like this
("sfalse4java")==s4
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhhhhhhhhhhhhhh my god!!.

Day by day I am learning new things and forgetting old things.......
Thank you James.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this s operator precedence..
== operator has less priority than +
and the expressions are evaluated from left to right..
so in Line1 is evaluated as false.
the expression is similar to :
(" 3 " + (s1==s3) + " 4 "+ s2) == s4
the result of this expression is false..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic