• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

working of post increment operator

 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • 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] ???

 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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;
 
Manoj Kumar Jain
Ranch Hand
Posts: 195
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbal, did you try to run the code ? play with it and you will come to know
 
Komal Arora
Ranch Hand
Posts: 91
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic