This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

doub in evaluation of assignment  RSS feed

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can any one explain me below code which expression will it evaluate first. What I expected was the answer -apple.

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Divya

If you see operator precedence, == have higher precedence than =.
So first 1 == 2 is evaluated which returns false and this value has been assigned to a.
Hence statement in else condition is executed.

Murali...
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey murali you said

1==2 returns false
and already value of "a =false"

than false=false,returns true isnt it?
or does it return false ?
and if it returns true than it should not go to
else part but print apple

and if false =false returns false
than only it should go to else part and print.....

please explain which is right?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically this is what happens.



  • As the operators work on Right-to-left, first the check on (1==2) is happening.
  • The result of the comparison is assigned to a boolean variable.



  • This is compatible and without compile-time error because the result of comparison (remember! == operator and NOT = operator) would always be of type boolean (true or false).

    Since the result of (1==2) is false (obviously! ), the same 'false' is assigned to the boolean variable 'a' (here you got to be careful, because you had used = operator). The result of an assignment will be the value of the expression.

    So, the value 'false' is assigned to 'a' and is checked in if loop thus making the 'else' statement eligible for execution. So, you get "banana" instead of "apples" contradicting to your guess!

    Just modify the same program and test:



    This will print "apple" with the same logic explained above.

    To just prove the result of assignment is the value returned, see the sample code below.



    The output of the above program is :



    The last line in the output clearly states and proves the value 'true' is being assigned and also checked for the same.

    Hope this helps!
     
    Rancher
    Posts: 42974
    76
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by dhwani mathur:
    than false=false,returns true isnt it?



    No. "=" is not a comparison, it's an assignment. Assignments return the value that gets assigned, which in this case is "false".
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well!!

    Thanks a lot Raghavan and UlfDittmer

    i got my doubt cleared......
     
    Divya Gehlot
    Ranch Hand
    Posts: 243
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    But in If condition we will check the condition if its true then execute if otherwise exceute else. here we instead of comparing we have assigned the value then why its going to else part.
     
    Ulf Dittmer
    Rancher
    Posts: 42974
    76
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Divya Gehlot:
    But in If condition we will check the condition if its true then execute if otherwise exceute else. here we instead of comparing we have assigned the value then why its going to else part.


    It is going to the "else" part because the condition is false. The condition in this case is the value of the assignment, which is false.
     
    Divya Gehlot
    Ranch Hand
    Posts: 243
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    now I got it thanks a alot
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!