• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

postfix vs prefix

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I was just testing out this piece of code and I was expecting the console to print out 20 for the variable, value and 16 for the variable, number.
I got 15 and 25 instead. I would appreciate if someone can help me figure out why this is the case.
Thank you!


 
Saloon Keeper
Posts: 26294
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prefix and postfix expressions have side-effects. Most of the time, these side effects can be tricky enough that you're best off using them only in simple expressions. Your example is a case in point.

The expression "int value = j * i++;" is the logical equivalent of "value = j * i; i = i+1". It's a postfix increment, which means that FIRST you return the value of I as it was (3) and THEN you increment (update) i itself (i = 4).

The expression "int number = ++i * j;" if a PREFIX increment, so its equivalent is "i = i+1; number = i * j". You increment i again (from 4 to 5), then multiply the updated value of i by j. Thus, 5 * 5 = 25.
 
Marshal
Posts: 76881
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Trent Davies
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim for the help.

I'm still wondering why you increment i again from 4 to 5 for the expression int number = ++i * j; ?

i = i + 1; //brings i from 3 to 4.

number = i * j; // shouldn't number be 4 * 5 = 20?

 
Tim Holloway
Saloon Keeper
Posts: 26294
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"number = i * j" would indeed be 4x5=20.

But the expression was "number = ++i * j", so first i would be incremented, then the updated value of i is used, resulting in 5x5=25.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be more specific, AFTER you compute value, i is now equal to 4. then when you compute number, you increment i again, and since it is a pre-increment, it becomes 5 before the multiply is preformed, to get 25 as the result.
 
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Trent, I think you have two misunderstandings based on your question.
1. Between post and pre increment operator
2. What happens to the variable once its post/pre incremented.

Lemme break it down for you-
int i = 3;
int j = 5;

int value = j * i++;
----- What you are expecting = (j*i)+1
= (5*3)+1
= (15)+1
= 16
----- What actually happens = (j*i) and i++
= (5*3) and i=3+1
= 15, and i is incremented to 4 only after j * i is done

int number = ++i * j;
----- What you are expecting = (1+i) * j
= (1+3) * 5
= (4)*5
= 20
----- What actually happens = (1+i) * j
= (1+4) * j, i was set to 4 in the previous line using the post increment operator and is no longer equal to 3
= (5)*5
= 25

I think it would be good if you read about post/pre increment/decrement operators again and try out some code with System.out.println statements to see how the value gets increased after the operations.
 
Sheriff
Posts: 8542
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trent Davies, welcome to the Ranch
Vaibhav Sagar, welcome too (by the way, have a cow for the putted effort in your answer)

Extra readings - read carefully this thread.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Trent Davies wrote:I was just testing out this piece of code and I was expecting the console to print out 20 for the variable, value and 16 for the variable, number.
I got 15 and 25 instead. I would appreciate if someone can help me figure out why this is the case.


Hi Trent, and welcome to JavaRanch.

I think everybody else has covered your main question, but the reason you're having problems to begin with is that combining increment operators with expressions is a really bad idea, so I'd get out of the habit of doing it as soon as you've taken whatever exam you're studying this for.

Winston
 
Tim Holloway
Saloon Keeper
Posts: 26294
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
....combining increment operators with expressions is a really bad idea, so I'd get out of the habit of doing it as soon as you've taken whatever exam you're studying this for.

Winston



One reason I've never been big on cert exams. When people dump code like that on me in real life, my first reaction is to (ahem) gently suggest that Bad Things will happen to them if they keep doing that. I don't have the patience to want to unsnarl it.

 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic