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
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# operators

Ranch Hand
Posts: 183
• • Number of slices to send:
Optional 'thank-you' note:
• • Q9. What is the output of running the following code (Select one Correct answer):
1. public class Q9{
2.
3. public static void main(String[] args){
4.
5. int i=3;
6.
7. System.out.println(i*=2 + i++);
8.
9. }
10.
11. }
Now in this sum the extended assignment has the lowest prcedence. So the right side is evaluated first So
1) 2 + 3(4)= 5
2) 4 * 5 = 20
So i becomes 20. So howcome 15. please explain

Ranch Hand
Posts: 75
• • Number of slices to send:
Optional 'thank-you' note:
• • Hi shabbir zakir:
1)int i=3;
2)i*=2*i++;
3) first the Expression will be evaluated.
4) 3*=2*3(4)
5) 3*=6
6) ans will be 18.
Siva.

Ranch Hand
Posts: 135
• • Number of slices to send:
Optional 'thank-you' note:
• • Hi Shabbir.Let's go through the code

1)The actual expression can be also written as

since we are using a shorthand operator.When this expression will be evaluated,the CURRENT value of i will be used.Hence the expression will be now i=3*(2+i++);
However,assignment has lower priority than the other operators used in the expression,thus the value 3 will be pushed on stack and the (2+i++) will be evaluated.
2)Now the expression (2+i++) has to be evaluated,which will come to 2+3=5,since a post-fix operator (i++) is used.Hence the expression (2+i++) evaluates to (2+3)
3)Value of i will now be incremented,because of i++.Hence now i=4.
4)Now the stored value is retrieved and it is multiplied with the result obtained in step 2) above.Thus,now the expression becomes i = 3*5, ie i=15, thus overwriting the value of i obtained in step 3) above.
I hope i have been clear enough.
------------------
Udayan Naik
Sun Certified Java 2 Programmer
[This message has been edited by Udayan Naik (edited March 01, 2001).]

Greenhorn
Posts: 22
• • Number of slices to send:
Optional 'thank-you' note:
• • Hello Dear
the answe is right it's 15,
i*=2 + i++;
1> i= 3 ;
2> i++ this is postincrement so value of i remains 3
3> 2 + i++ = 5,
4>i*=5 (now its become 5) so its evaluted as i = i * 5 i.e.(i= 3 * 5) = 15,
if it was like this i*=2 + ++i;
than
1> i = 3;
2>++i= 4 (because its preincrement but i remains 3 only);
3> 2+4 = 6;
4>i*= 6 (so its become i = 3 * 6 )
5> 18
so in this case it will print 18,
correct me if i am wrong.
Love
Lokesh

shabbir zakir
Ranch Hand
Posts: 183
• • Number of slices to send:
Optional 'thank-you' note:
• • Thanks a lot. Now i understand what have been forgetting. Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one: Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat
• 