• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

arithematic expression

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Noam. Will Eclipse be good ,or should i get any other( actuallly i have no idea about IDEs)?
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
vaibhav mishra
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand your point and with little digging through JLS I found that I lack this basic concept
Thanks for pointing it out
 
What's a year in metric? Do you know this metric stuff tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic