• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Operator doubt

 
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The following code snippet prints 15. I was expecting it to print 16.


It seems like it is not using the post increment (s++) to increment s by 1 after evaluating s as (s=5+5+5 = 15). This is surprising to me as s is being printed in the next line and I thought that would be enough for the post increment to kick in before the printing occurred. Could someone please clarify my doubts here ?
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The post-increment operator executes after the expression is evaluated. It executes after s is assigned its new value. It can be re-written as follows.

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

Asad Zubair wrote:Hi,

The following code snippet prints 15. I was expecting it to print 16.


It seems like it is not using the post increment (s++) to increment s by 1 after evaluating s as (s=5+5+5 = 15). This is surprising to me as s is being printed in the next line and I thought that would be enough for the post increment to kick in before the printing occurred. Could someone please clarify my doubts here ?



Hello Asad
try to put the whole expression in System.out.println() you will get 15 as a result.

System.out.println(s+=s+s++);
 
Asad Zubair
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, I believe you have missed the point in my question. Why does my code print 15 and not 16 ? Should the post increment not kick in after line 2 is finished but before line 3 is executed ?

Ryan, your re-written code is printing 16, not 15. Even though I thought it would execute the same way as my code (which prints 15)
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you check the Java Language Specification, you'll find the following under "Compound Assignment Operators".

First, the left-hand operand is evaluated to produce a variable...

...the value of the left-hand operand is saved and then the right-hand operand is evaluated...

...the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator


In other words, it uses the value of s at the start of processing (5) to add to the result of the right-hand operation (10) to produce the final result to assign (15). The incremented value is therefore not used in the calculation, and is overwritten by the final assignment.
 
Asad Zubair
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew,
It still does not make total sense to me why the post increment gets overwritten rather than being evaluated after line 2 completes. However, looks it is a JLS rule and I just need to burn it in for the exam.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think that s += s + s++; is the same as say s = s + (s + s++);

step 1: s = s + (s + s++);
step 2: s = 5 + (5 + 5);
step 3: s = 5 + (10); // in this moment, do s++ and s = 6
step 4: s = 15 // overwritting the value of 6 that has 's'

I hope help you.

Sorry for my english
 
Asad Zubair
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I start to play with the code a bit more, it makes more sense now. The execution happens differently for comparison operators vs assignment operators (due to the over-writing concept). I previously thought it was the same. Anyways, nice little exam gotcha that I should be prepared for now. Thanks for the comments !


 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic