This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes working of post increment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "working of post increment operator" Watch "working of post increment operator" New topic
Author

working of post increment operator

Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
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

Joined: Aug 11, 2012
Posts: 370
    
    1

See UseCodeTags

This question have been asked numerous times..
Check thisFAQ for your doubt..
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
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

Joined: Aug 11, 2012
Posts: 370
    
    1

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

Joined: Aug 22, 2008
Posts: 191

Rubbal, did you try to run the code ? play with it and you will come to know


Do not wait to strike till the iron is hot; but make it hot by striking....
Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

yeah run the code. that would make all things clear.

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





OCPJP
Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: working of post increment operator
 
Similar Threads
Compound additive operator and post increment
Is this java's Bug
doubts getting exponentially INCREMENTed
here is a question and i have a very similar code but i need to convert it
K&B Book for Scjp 6 - Chapter 5 question 8 doubt