Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# arithematic expression

gylph knor
Greenhorn
Posts: 20
can anybody please explain that how the output is being generated here:

int a = 2;
a += a++;
a++;
System.out.println(a);

output:5

Noam Wolf
Ranch Hand
Posts: 35
Hi Gylph,

I highly recommend you get an IDE that supports debugging. Write this program and step through execution while watching the variable "a". That will give you a real good idea of what's going on.

gylph knor
Greenhorn
Posts: 20
Thanks Noam. Will Eclipse be good ,or should i get any other( actuallly i have no idea about IDEs)?

vaibhav mishra
Ranch Hand
Posts: 168
I compiled above code and get output 5

then I compile this code

and got output as 4

then I compile this code

and get same output i.e., 4

it seems that execution of
a+=a;
and
a+=a++;
results same

Ireneusz Kordal
Ranch Hand
Posts: 423
int a = 2; // line 1
a += a++; // line 2
a++; // line 3
System.out.println(a);

line 2:
a += expr is equivalent to:
a = a + expr
so line 2 is:
a = a + a++

expression a++ (postincrement operator ++) evaluates to 'a' (rerurns value of a), then increments 'a' by 1 (after this a==3, but expression a++ returns 2)

Whole expression evaluates to: 2+2, and this value is assigned to a (3 is replaced with 4)

In line 3 'a' is incremented by 1 and is now 5

[ June 22, 2008: Message edited by: Ireneusz Kordal ]

[ June 22, 2008: Message edited by: Ireneusz Kordal ]
[ June 22, 2008: Message edited by: Ireneusz Kordal ]

vaibhav mishra
Ranch Hand
Posts: 168
if
then from my understanding it seems that the order of eveluation is as follows
(a=a+(a))++
and so it will give a=5
what you said is that it wil be something like this
a*++;
a=a+a*;
where starred a is same a but representing the one with a++
and it seem to give also 5

which means certainly that I am not able to understand what you said
however thanks for response.

Ranch Hand
Posts: 802
int a = 2;
a += a++;
a++;
System.out.println(a);

when a+=a++ expression executed
it always executed as follows

consider following steps

variable += expression
when above line execute the following thing happens
temp variable = variable
right hand expression fully evaluated
then evaluated expression is added to temp variable
which has original value of variable
then result is assigned to lefthand side variable
(java language specification third edition has given information on that)

as explained above steps are performed to expression a+=a++
first original value of variable a is placed in temp variable
then value of right hand expression evaluated
and evaluated expression value added to temp variable value that is original value of a
and then result is assigned to variable a on lefthand side

this is like a = (a has value 2)+(increamented value of a that is 3)
therefore a = 2+3
so output is 5

hope this helps

Regards
[ June 22, 2008: Message edited by: Ninad Kulkarni ]

vaibhav mishra
Ranch Hand
Posts: 168
you are right and I calculated this but the expression evaluated to 4

and then next line

make it 5

so make it clear again

and I referred JLS 3rd edition and following line might be what you are referring

Thanks and regards
Vaibhav Katyayan

[ June 22, 2008: Message edited by: vaibhav katyayan ]
[ June 22, 2008: Message edited by: vaibhav katyayan ]

Ranch Hand
Posts: 802
Read JLS completely if not complete abrubtly then increament occurs
there are all possible situation given in document
if complete abruptly then no increament but if complete normally then increament occurs

a += a++;

original variable = (temp variable which hold initial value of a) + (already evaluated expresion)

let see step by step
initially a = 2
when a += a++; this statement executed
first vaue of a(value of 2)is copied to temp variable created by compiler
then righthand side of expression evaluated normally not abruptly
(here value of a becomes 3)
then value of expresiion (value 3)is added with temp variable(value of 2) not with value of a (which is increamented to 3)
then obtained result is assigned to variable a

a = temp variable + 3

hope this clears

Regards

[ June 23, 2008: Message edited by: Ninad Kulkarni ]
[ June 23, 2008: Message edited by: Ninad Kulkarni ]

Ranch Hand
Posts: 802
yes you are correct ouput is 4
I will check myself and I reply to post as early as possible

but let you can compile following code and tell me what happen

int a = 2;
a=a++;
System.out.println(a);

which output occurs 2 or 3?
I have already explained these but check it for practice

Regards

Ranch Hand
Posts: 802
JLS third edition page no 486

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

because of this output is 4

thanks for clearing me for that

you can compile and run the following also

int a = 2;
a+=++a;
System.out.println(a);

you get 5 as output

Thanks and regards

Ranch Hand
Posts: 802
you can try following two code fragments

1)int a = 2;
a += a += a += a++;
System.out.println(a);

output is 8

2)int a = 2;
a += a += a += ++a;
System.out.println(a);

output is 9

just compile and run the above code fragments

Regards