• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question from K&B Book

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this question is from pg 200 Question 12 from K&B book

the ans is C. count = 3
when i first do the question, i get count = 4, the reason being i evaluated this code first, (++count - 2).
however the explanation is that it is a short ciruit expression thus, the left-hand side will be evaluated first, since it evaluated to false the right hand expression would not be executed.
So my question is this, arent expression in () always evaulated first? or am i mistaken?
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The variable count is equal to 3 before the if expression
is evaluated, so the short circuit operator rule that the
right hand side is not evaluated holds in this case as
well. Please replace the void test() method with:

Running the main will result in:
C:\projects\misc>java BoolArray
b[0] = true
b[1] = false
b[2] = true
Before If count = 3
After If count = 3

Does this answer your question?
Cheers,
Gian Franco Casula
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have changed the code a bit:

The first condtion gets excuted.
b[0] = true
b[1] = false
so b[0] && b[1] = false
then b[2] = true
so b[0] && b[1] | b[2] = true
and thus count gets incremented
 
Shilpi M Ag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addtion to what i said above:

in this bit of code cince b[0] == false, the compiler does not go ahead
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general they are, but when used in combination with &&, || and ?: this is not the case.
The Java Language Specification has a very clear section on this. You could check out paragraph 15.7 of the JLS. It's a very short and not so technical explanation.
 
guan di
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actaully, i do know the short circuit rule, but i mistakenly thought that code in () will always be executed first....
thanks for the replies and explanations, are there any other cases beside short-circuit logical expressions where code in () are not executed first?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have, for example,

the (++i) and (i++) would not get evaluated first. The "j = ..." expression is evaluated left to right.
So, in this case, the i++ returns 0, but sets i to 1. The (++i) returns and sets i to 2. The (i++) returns 2 and sets i to 3. This is another case where code in () does not get evaluated first.
[ May 08, 2004: Message edited by: Jong Limb ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic