whats the Reason behind this ?

int i=2;i=i++;/// i tht that this statement means assign the value of i and then increment the value of i ,but why is it not working ?JAVA : System.out.println("i="+i);output :i=?c/c++ : printf("%d",i);output :i=?(o/p's are different ..... why?)
[295 byte] By [bpcraoa] at [2007-10-2 21:18:56]
# 1
C++ is not Java. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arithmetic.html
YoGeea at 2007-7-14 0:28:01 > top of Java-index,Java Essentials,Java Programming...
# 2

First of all, I hope you understand that code like "i = i++" should never be used in a real world program, because it's confusing and most likely wrong. If you want to increment the value of i, you have to write just "i++" instead.

If you want to know why this:

int i = 2;

i = i++;

produces i = 2, look here: [url=http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment]Post increment operator and assignment[/url]

Why it is different from C / C++: Because Java is not C / C++ and the rules are just defined differently in Java.

jesperdja at 2007-7-14 0:28:01 > top of Java-index,Java Essentials,Java Programming...
# 3
ya fine i know that , i just want know the rule behind this
bpcraoa at 2007-7-14 0:28:01 > top of Java-index,Java Essentials,Java Programming...
# 4
yes according to the link you gave it says "++op++Increments op by 1; evaluates to the value of op before it was incremented " but this very rule is not followed
bpcraoa at 2007-7-14 0:28:01 > top of Java-index,Java Essentials,Java Programming...
# 5
> yes according to the link you gave it says > "++op++Increments op by 1; evaluates to the> value of op before it was incremented " > > but this very rule is not followedi++ is not the same as ++iKaj
kajbja at 2007-7-14 0:28:02 > top of Java-index,Java Essentials,Java Programming...
# 6
i know i ++ is post increment and ++i is pre increment why is it not working ?
bpcraoa at 2007-7-14 0:28:02 > top of Java-index,Java Essentials,Java Programming...
# 7
i=0;i=i++; should mean i is assigned the value of i then its 0 and then i should be incremented right so it should be 1 when i try to print why is it not happening ?
bpcraoa at 2007-7-14 0:28:02 > top of Java-index,Java Essentials,Java Programming...
# 8
Why are u assigning the value of i to i itself
Karthikeyan_Vaithilingama at 2007-7-14 0:28:02 > top of Java-index,Java Essentials,Java Programming...
# 9
leave why i am assigning , but there should be some reason behind this the rule defined should work ,
bpcraoa at 2007-7-14 0:28:02 > top of Java-index,Java Essentials,Java Programming...
# 10

> i=0;

> i=i++;

> should mean

> i is assigned the value of i then its 0 and then

> i should be incremented right

> so it should be 1 when i try to print

> why is it not happening ?

No

The expression i++ is evaluated, and the current value of i is saved for later.

i is incremented to 1

The saved value prior to the increment (0) is assigned back into i

The code is a complete waste of time. However, it teaches the value of understanding post-increment.

roger.bagnalla at 2007-7-14 0:28:02 > top of Java-index,Java Essentials,Java Programming...
# 11

This is well defined in the JLS (Java Language Specification). The pertinent section can be found at http://java.sun.com/docs/books/jls/first_edition/html/15.doc.html#39438

...The value of the postfix increment expression is the value of the variable before the new value is stored.

What you are doing is no different than the following:

x = 0;

y = x++;

paternostroa at 2007-7-14 0:28:02 > top of Java-index,Java Essentials,Java Programming...
# 12
> leave why i am assigning,No, I want to know why you are doing this.
tschodta at 2007-7-14 0:28:02 > top of Java-index,Java Essentials,Java Programming...
# 13
Post increment will not be concider when u assign the value to increment after the statement. U set the i as i so it wont incremented
Karthikeyan_Vaithilingama at 2007-7-14 0:28:02 > top of Java-index,Java Essentials,Java Programming...
# 14
i tht thats the reason any proof as such from sun java ? or defined anywhere ?
bpcraoa at 2007-7-14 0:28:02 > top of Java-index,Java Essentials,Java Programming...
# 15
"The value of the postfix increment expression is the value of the variable before the new value is stored."the above statement means x=i++ the value of x is i before the increment
bpcraoa at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 16
> i tht thats the reason any proof as such from sun> java ? or defined anywhere ?*sigh* Can't you read?Read reply #11 by paternostro Kaj
kajbja at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 17

"The value of the postfix increment expression is the value of the variable before the new value is stored."

fine then according to this the value of the exp is the value of the

variable before tha value is stored

so the value of i is value of i before the increment

and anyhow after that the value is incremented and then new value is stored yes or no ?

Message was edited by:

bpcrao

bpcraoa at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 18

what you need to understand is that the right hand side gets evaluated FIRST, before the result is applied to the left hand side.

so, the value of i does get incremented to 3, but then the i++ has a return value of 2, which gets assigned back to i.

you actually have two assignments occuring here. the first is an incrementation, and the second is assigning the return value of the incrementation, which is the same as the original value.

- Adam

guitar_man_Fa at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 19

> i tht thats the reason any proof as such from sun

> java ? or defined anywhere ?

paternostro quoted the official Java Language Specification above, which describes the official way this should work.

It does not work the way you think it should work. Why? Because the Java Language Specification says it works differently!

Continue complaining about it as you wish, but it's not going to make any difference! It works as it is supposed to work according to the official specification.

The specification says: i++ returns the old value of i (the value before it was incremented.

So if you do i = i++, the following steps are happening:

1. The value of i is incremented from 0 to 1.

2. The old value (0) is assigned to i.

The end result is that i is 0.

It is not difficult to understand.

jesperdja at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 20

> i know i ++ is post increment and ++i is pre

> increment

> why is it not working ?

It's working perfectly. It's behaving exactly as defined by the JLS.

i++ is an expression. It has a value and a side-effect. The value of the expression is the original value of i.

When you do <some_variable> = i++;

the variable will be assigned the value of i before it is incremented. This is true even if the variable is i itself.

i = i++;

is equivalent to

tmp = i; // save original value of i

i += 1; // add one to i (the ++ part)

i = tmp; // put the original value of i into the variable on the left side.

I'm sure this has all been explained elsewhere in this thread though.

jverda at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 21

> "The value of the postfix increment expression is the

> value of the variable before the new value is

> stored."

>

> fine then according to this the value of the exp is

> the value of the

> variable before tha value is stored

>

> so the value of i is value of i before the increment

> and anyhow after that the value is incremented and

> then new value is stored yes or no ?

>

Why would the new value be stored? "The value of the expression" is what's stored. That's what's meant by "the value of the expression". It means that, regardless of any side-effects, i++ itself has a value and if you stick in on the RHS of =, or as an arg to a method or c'tor, then that value--the expression's value--is what will be seen.

jverda at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 22
Oh, and, although this is precisely defined in the JLS, in the C++ spec, the order of evaluation is not defined. Some compilers will give the same behavior as Java, and some will not.
jverda at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 23

to put this topic to rest, take a look at an explanation from a True Java Expert, Joshua Bloch.

He covered this in his programming puzzlers talk at the 2005 Java One conference (TS-3738). If you view the media presentation, he will explain to you what the problem is. You can find it at http://developers.sun.com/learning/javaoneonline/2005/coreplatform/

Look at puzzle number 3 (Tricky Assignment).

guitar_man_Fa at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 24

int x = 0;

int i = 0;

x = i++;

it mean..

Ref = i;( Ref = 0 )

i = i+1;( i = 1 )

x = Ref;( x = 0 )

result x = 0 and i = 1

int i = 0;

i = i++;

it mean..

Ref = i;(Ref = 0 )

i = i+1;(i = 1 )

i = Ref;(i = 0 )

resutl i = 0;

Aae2kmitla at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...
# 25
Why did you post this in a 6-month old thread when it's already been thoroughly answered?
jverda at 2007-7-21 1:58:19 > top of Java-index,Java Essentials,Java Programming...