• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

working of post increment operator

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt regarding the working of post increment operator, as far as I know about post increment operator, it first uses the value, and then increments the value.
E.G. int x, a, b;
a = 10;
b = 25;
x = a + b++; // here in the expression the it uses the original value of b and then increments it by 1 i.e. it takes it as 10 +25 and then increases the value of b to 26.
System.out.println("The final value of x is : " + x);
BUT BUT BUT
in the code given below I want to know how it behaves in an array ???

class Array_chk {
public static void main(String[] args) {
int [] arr = new int[3];
int x = 0;
while(x < 3){
arr[x++] = 1; // whether it assigns 1 to arr[o] and then increments the value of x or it skips the arr[0] and directly assigns 1 to arr[1] ???
System.out.println("the printed element is :" +arr

[x]);
}
}// end of main
}// end of class

My question is :
whether it assigns 1 to arr[o] and then increments the value of x or it skips the arr[0] and directly assigns 1 to arr[1] ???

 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See UseCodeTags

This question have been asked numerous times..
Check thisFAQ for your doubt..
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Jain wrote:See UseCodeTags

This question have been asked numerous times..
Check thisFAQ for your doubt..



The link you shown doesn't tell exactly about the behavior in arrays otherwise that I already know as I also gave an e.g. of simple execution of post-increment operator .
I want to know the behavior in arrays , the code I wrote on an array .
Could you please explain about that ???
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rubbal Bhusri wrote:

R. Jain wrote:See UseCodeTags

This question have been asked numerous times..
Check thisFAQ for your doubt..



The link you shown doesn't tell exactly about the behavior in arrays otherwise that I already know as I also gave an e.g. of simple execution of post-increment operator .
I want to know the behavior in arrays , the code I wrote on an array .
Could you please explain about that ???


It doesn't make any difference while working with array..
arr[x++] = 1 assigns 1 to arr[0]..

I gave you that link because the first statement you wrote is quite common understanding amongst all but not correct..
You wrote: -

Rubbal Bhusri wrote:as far as I know about post increment operator, it first uses the value, and then increments the value.


But, in actual, it first increments the value, but before incrementing it stores the value for future use..
Now after it is done with incrementing,uses the value previously stored for assignment..
You see the difference..

So ideally, if you have i = 0 and , if you write : -
i = i++;
You still have i = 0 after this statement, even though you are sure it will be incremented after the semi-colon(;)

This is because the above statement is executed as follows: -

tempVarByJVM = i;
i++;
i = tempVarByJVM;

So, i = 0;
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rubbal, did you try to run the code ? play with it and you will come to know
 
Ranch Hand
Posts: 91
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah run the code. that would make all things clear.

For arrays, the rules remain the same.
try out this:



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

R. Jain wrote:
arr[x++] = 1 assigns 1 to arr[0]..



This was what I wanted to confirm from you guys.
I guess, I got my answer , I am very thankful to all of you guys for helping me out.
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic