aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes arithematic expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "arithematic expression" Watch "arithematic expression" New topic
Author

arithematic expression

gylph knor
Greenhorn

Joined: Jun 12, 2008
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




Thanks in advance!!!
Noam Wolf
Ranch Hand

Joined: Jan 12, 2008
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.


because .net guys can also write in java
gylph knor
Greenhorn

Joined: Jun 12, 2008
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

Joined: Jun 18, 2008
Posts: 168
will somebody please explain
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

please elaborate.


SCJP
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
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

Joined: Jun 18, 2008
Posts: 168
I somewhat understand your reply but still I am somewhat confused
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
please explain more clearly
however thanks for response.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

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
Ninad
[ June 22, 2008: Message edited by: Ninad Kulkarni ]

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
vaibhav mishra
Ranch Hand

Joined: Jun 18, 2008
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
please explain this line

Thanks and regards
Vaibhav Katyayan

[ June 22, 2008: Message edited by: vaibhav katyayan ]
[ June 22, 2008: Message edited by: vaibhav katyayan ]
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

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
Ninad

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

Joined: Aug 31, 2007
Posts: 797

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
Ninad
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

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
Ninad
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

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
Ninad
[ June 23, 2008: Message edited by: Ninad Kulkarni ]
vaibhav mishra
Ranch Hand

Joined: Jun 18, 2008
Posts: 168
I understand your point and with little digging through JLS I found that I lack this basic concept
Thanks for pointing it out
 
 
subject: arithematic expression