File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why i=i++; doesn't get incremented 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 "why i=i++; doesn Watch "why i=i++; doesn New topic
Author

why i=i++; doesn't get incremented

Manwarjit Singh
Greenhorn

Joined: Jan 08, 2001
Posts: 6
HI,
In following code, Why does't value of i get incremented :
_______________________________________________
public static void main(String args[])
{
int i=9;
i=i++;
System.out.println(i);
}
}
_______________________________________________
output is : 9
Where as it should be 10.
-Thanks-
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi,
We know the post increment operator means to use the current value and then perform the increment. In you example:
1. Copy current value of i to memory location, M1: 9
2. Increment value that i is referencing: i --> 9+1 = 10
3. Perform assignment of M1 to i: 9
Result, i --> 9!
It even gets more interesting if you had written:
int i = 0;
i = i++ + i++;
Then we get:
1. Copy current value of i to memory location M1: 0
2. Increment current value of i: 0+1 = 1
3. Copy current value of i to memory location M2: 1
4. Increment current value of i: 1+1 = 2
5. Perform binary addition on M1 and M2: 0 + 1 = 1
Regards,
Manfred.
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
Hi.
Can you help with how to resolve silly/confusing areithmatics like
b=a+++c-2++++d-++k
Que 1. What's the logic to resolve these computations using operator precedence?
Que 2. Do we get questions like this in Certification exams ?

------------------
http://www.mantrotech.com


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Gaurav,
Read Maha Anna's answer to solving these type of questions in http://www.javaranch.com/ubb/Forum24/HTML/000775.html .
If you can solve these you won't have a problem on the exam, luckily, the questions aren't quite so convoluted.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Manwarjit Singh
Greenhorn

Joined: Jan 08, 2001
Posts: 6
Thanks Manfred for clearing my dbout.
[This message has been edited by Manwarjit Singh (edited February 16, 2001).]
[This message has been edited by Manwarjit Singh (edited February 16, 2001).]
Wasim Ahmed
Ranch Hand

Joined: Jan 31, 2001
Posts: 90
Answer is yes, atleast one question. They want to know your understanding of Operator pre-increment /post-increment. Here is the easy method that I have learnt and always works.
int i = 1
i= i++ + i++ + i
answer is 6. How ?
put increment operator in inthe prenthesis.
i = 1(2) + 2(3) + 3
value of i changes when previou expression perform the increment operation but it doesn't get executed. now drop the values from the prenthesis.
i = 1 + 2 + 3 = 6
I hope this will help
Manwarjit Singh
Greenhorn

Joined: Jan 08, 2001
Posts: 6
Thanks Wasim, Jane
Similarly we can do for post/pre, increment/decrement :-
Here is the example :-
_____________________________________________
public class M{

public static void main(String args[])
{
int i=1;
i = i++ + ++i + i + --i + i--;

// i = 1(2) + (3)3 + 3 + (2)2 + 2(1)
// now remove all non-barckets .
// So we get
// i = 1+3+3+2+2
// i = 11;

System.out.println(i);
}
}
_____________________________________________
Output is 11.
[This message has been edited by Manwarjit Singh (edited February 16, 2001).]
Bhasin Sameer
Greenhorn

Joined: Feb 15, 2001
Posts: 4
hi
see I feel i++ means i=i+1;
applying logic as mentioned above i think the question should be really explained well.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Manwarjit,
It seems you still don't get it. My answer for i = i++ + i++ is correct. I even showed you step by step how it works! The program below will prove me right!

correct this time,
Manfred.
Manwarjit Singh
Greenhorn

Joined: Jan 08, 2001
Posts: 6
Originally posted by Manfred Leonhardt:
[B]Hi Manwarjit,
It seems you still don't get it. My answer for i = i++ + i++ is correct. I even showed you step by step how it works! The program below will prove me right!

Sorry, I was calculating with i = 1 , But urs is with i = 0;
Its altright, I withdrew my comment.
correct this time,
Manfred.[/B]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why i=i++; doesn't get incremented