• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Another question from dan's test

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the above code += has lower priority than increment operators.so it should be evaluated the following way
a += 2+2(3)
a=(int)3+2+2
a=7
But the above code prints 5 a
a=(int)1+2+2
a=5
If we exapand += operator before increment operator doesn't that mean += operator is given more priority than ++ operator? ,which ofcourse is not true.Can someone clear my doubt?
Thanks
Veena
 
Ranch Hand
Posts: 8946
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JLS

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. Note that the implied cast to type T may be either an identity conversion (�5.1.1) or a narrowing primitive conversion (�5.1.3). For example, the following code is correct:
short x = 3;
x += 4.6;
and results in x having the value 7 because it is equivalent to:

short x = 3;
x = (short)(x + 4.6);

 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeep
Actually what i meant is += operator is evaluated first before unary ++ opearator....which is not technically correct,coz ++ opearator has higher priority than +=...
Veena
 
Pradeep bhatt
Ranch Hand
Posts: 8946
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The += evaluation takes place the last.
a+= b+c
is expanded to
a =a + (b+c)
b+c is evaluated first and then a is added to it => += evaluation takes place last.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Veena Point:
[QB]
In the above code += has lower priority than increment operators.so it should be evaluated the following way
a += 2+2(3)
a=(int)3+2+2
a=7

In the above code += operator is not given higher priority it is evaluated after increment operator.
It is evaluated as under:
a(1) += ++a(2) + (2)a++
a(1) += 4
a = 1 + 4
a=5
I hope it will help you.

 
Kashif Memon
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Veena Point:

In the above code += has lower priority than increment operators.so it should be evaluated the following way
a += 2+2(3)
a=(int)3+2+2
a=7
Veena



In the above code += operator is not given higher priority it is evaluated after increment operator.
It is evaluated as under:
a(1) += ++a(2) + (2)a++
a(1) += 4
a = 1 + 4
a=5
I hope it will help you.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you .It helped.I think operands are evaluated first(from left to right) & then expression is evaluated.Am I right?
Veena
 
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic