• 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

confused with comparison and assignment in same line

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


All:

The result of this code is "false". OK, so please tell me how the compiler analyzes line 1. It obviously did not execute "b1 = i1" first as if it had there would have been a compiler error generated. So can someone please enlighten me as to how the compiler will analyze and execute line 1? When it sees code such as that will it ALWAYS complete the "i1 == i2" first? And, even though it would cause a compiler error, why did it not try to execute "b1 = i1" first? Is there some rule that I should know as to how the compiler will interpret such code as that found on line 1?

thank you
Gary
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gary Marshall wrote:

The result of this code is "false". OK, so please tell me how the compiler analyzes line 1. It obviously did not execute "b1 = i1" first
...
And, even though it would cause a compiler error, why did it not try to execute "b1 = i1" first?



"if (b1 ="

At this point, we have the boolean variable b1 and a symbol (the equals sign) indicating that something will be assigned as the value of that variable. But what will be assigned? Everything to the right of the '=' will be evaluated (not executed[i]) and assigned to b1.

So then, what's to the right of "b1 =" ? An expression of the form

"i1 == i2"

This expression is a claim that the value of i1 is the same as the value of i2 (on the relevant definition of "same as"). That claim is either true or false. Since it's false, "false" is the value assigned to b1.

The compiler doesn't choke on "b1 = i1" because of guarantees in the language spec about the order of evaluation. Evaluation is from left to right. All operands will be evaluated before an operation on them is evaluated. In case there are multiple operators, the leftmost value will be defined and retained before whatever lies to the right. Here's the skinny: link.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gary you can check the operator precedence table in such cases...
 
reply
    Bookmark Topic Watch Topic
  • New Topic