This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  assignment operator  with post increment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " assignment operator  with post increment " Watch " assignment operator  with post increment " New topic
Author

assignment operator with post increment

Ananth Majumdar
Greenhorn

Joined: Aug 17, 2006
Posts: 20
public class Precedence {

final public static void main(String args[]) {

int i = 0;

i = i++;

i = i++;

i = i++;

System.out.println(i); }

}
This should print 3 but it is printing 0. Please explain
[ March 27, 2007: Message edited by: Ananth Majumdar ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by Ananth


public class Precedence {

final public static void main(String args[]) {

int i = 0;

i = i++;

i = i++;

i = i++;

System.out.println(i); }

}


You are working with post increment operator which is incremented after the variable is used. Consider you are a assigning the i to itself. So each time i is getting zero. If you assign "i" to any other variable you will get the value of "i" what you expect "3"

int i =0;
i=i++; //i is assigned 0
i=i++; //i is assigned 0
i=i++; //i is assigned 0

System.out.println("i = " + i); //prints 0



Hope this helps you!

Thanks and Regards,
cmbhatt


cmbhatt
Ananth Majumdar
Greenhorn

Joined: Aug 17, 2006
Posts: 20
i++ actually means that i=i+1; isn't it? So for i=i++

first
i=0; is assigned then i++ happens which is i=i+1; i.e i=1; so it should be 1. Why is it zero I am not able to understand. Please clarify..
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
If you independently write i++; it means i=i+1;

int i=0;
System.out.println("i = " + i++); // prints 0
System.out.println("i = " + i++); // prints 1
System.out.println("i = " + i++); // prints 2

System.out.println("i = " + i); //prints 3

// post increment only increments the value after the variable has been used.

What your case says : You are assigning the value of i++ to the same variable "i" so i stays 0 each time in assignment operation.

i++ does mean i=i+1; only and only when you keep it independent;
int i=0;
int j=i++; // j would be assigned 0 and then i would be i=i+1 //=1
j=i++; // j would be assigned 1 and then i would be i = i+1 //=2
j=i++; // j would be assigned 2 and then i would be i = i+1 //=3

What your case says:

int i=0;
i = i++; // assign 0 to i and operation completes
i = i++; // assign 0 to i and operation completes
i = i++; //assign 0 to i and operation completes
System.out.println("i = " + i);


Did you get that?

Thanks and Regards,
cmbhatt
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Chandra Bhatt

That means the i value will not increment ?
am i write
Ananth Majumdar
Greenhorn

Joined: Aug 17, 2006
Posts: 20

int i=0;
i = i++; // assign 0 to i and operation completes
i = i++; // assign 0 to i and operation completes
i = i++; //assign 0 to i and operation completes


i=i++ // assign 0 to i and operation completes then when does i++ happen?
If the increment happens after the first i=i++ why is it not one atleast for the second assignment i=i++
Ananth Majumdar
Greenhorn

Joined: Aug 17, 2006
Posts: 20
when the expression completes why would i be incremented?


if i++ is not done at all then what is the difference between i=1; and i=i++;

What I supposed was even if it is not used for this assignment, it would be incremented by the time the variable is used in the next expression which means atleast it should be used in the second assignment. Please clarify
why i++ is not executed..
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
i=i++;

Ok, let me explain it in detail


What happens with that code is this:

Let's say 'i' has a starting value of 0. The expression 'i++' evaluates to the pre-increment (original) value of 'i', or 0. The result of this expression (0) is remembered. The value of 'i' is then incremented ('i' is now 1). The '=' assignment operator has the least precedence, so it happens last: the result of the right-hand expression (what we remembered earlier, namely 0) is assigned back to 'i'. Therefore, the result of the whole statement is that 'i' has a value of 0.


Now, did you get this?

Thanks and Regards,
cmbhatt
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
Sequence is as follows.
1. Left side variable i reads the value from the right side variable(i value 0).
2.Checks the end of expression and right side variable i gets incremented by Post increment operator(i=1) .
3. Left side variable is assigned with value it read before in step1 and hence overwritten with 0.
Ananth Majumdar
Greenhorn

Joined: Aug 17, 2006
Posts: 20
Thanks chandra and kumar..I got it know..
 
GeeCON Prague 2014
 
subject: assignment operator with post increment