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

# ++/-- operator

Ranch Hand
Posts: 54
• Number of slices to send:
Optional 'thank-you' note:
Can someone please tell me why this code prints out a value of 3.
public class Static {
static int x, y;
public static void main( String[] args) {
x--;
System.out.println(x);
myMethod();
System.out.println(x + y + ++x);
} // end main(String[])
public static void myMethod() {
y = (x++ + ++x);
System.out.println(x);
} // myMethod()
} // end Static

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
++x is a pre increment
and x++ is a pos increment
So,
y=(x++ + ++x) it is equal y=(-1 + 1)
and
(x + y + ++x) it is equal (1 + 0 + 2)
[ June 24, 2003: Message edited by: Maximiliano Guzenski ]

Ranch Hand
Posts: 1090
• Number of slices to send:
Optional 'thank-you' note:
Hi Weibust
Let's number a few locations and see how the code gets executed.

First the 1 is executed which causes x to be -1. Then 2 (a call to myMethod() method) is executed. The method's expression y=(x++ + ++x) evaluates to y = (-1 + (+1)); So y becomes 0 and x is +1. Now 4 is executed which prints out the x value. Then 3 is executed System.out.println(x + y + ++x); which resolves to System.out.println(1 + 0 + 2); which is 3.
[ June 24, 2003: Message edited by: Anupam Sinha ]

E Weibust
Ranch Hand
Posts: 54
• Number of slices to send:
Optional 'thank-you' note:
I was thinking that if it was a post++ the increment didn't occur until after the expression was evaluated. Looking like:
y = (-1 + (0));
With y getting set to -1 and then x++ occuring and bumping x to 1.

Author
Posts: 201
• Number of slices to send:
Optional 'thank-you' note:
Rewrite the code in a simplier way like: Example

For the problem:

so after this step , y = 0, x = 1 which is everyone else said.
I tell my students if the can figure out complex ++/-- in their heads- rewrite it a simplier way to understand it.
[ June 24, 2003: Message edited by: James Chegwidden ]
[ June 24, 2003: Message edited by: James Chegwidden ]
[ June 24, 2003: Message edited by: James Chegwidden ]

Author & Gold Digger
Posts: 7617
6
• Number of slices to send:
Optional 'thank-you' note:
The following discussions might help:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
https://coderanch.com/t/239576/java-programmer-SCJP/certification/result

E Weibust
Ranch Hand
Posts: 54
• Number of slices to send:
Optional 'thank-you' note:
Mr C,
Thanks for clearing that up. Your approach was very useful.
Erik

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.