• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Operators

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Q7
{
public static void main(String[] args)
{

int i=3;
System.out.println(ii*=2 + i++);
}
}

I came across this question.When I ran this o/p is 15.How can o/p is 15.

Please tell me the logic.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm assuming the 'ii' is a typo (else it won't compile), and that the line actually reads as follows:

If so, the logic is as follows:

i = 3 * (2 + 3); // i++ evaluates to the original value of i
i = 3 * 5;
i = 15;

Does that help? I can probably guess the next question, though. It's, "where did my incremented value go?", right? Good question!

It helps to understand the difference between variables and an operand stack. I'll write up the steps from a bytecode point of view...



Hope this helps...
[ October 31, 2005: Message edited by: Steve Morrow ]
 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I got it.I was confused ny i++.

Thanks for detailed explanation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic