aspose file tools*
The moose likes Beginning Java and the fly likes incremetor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "incremetor" Watch "incremetor" New topic
Author

incremetor

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Here are two programs using the incrementor x++ or x-- in a different way:
Program1:
class incDec
{
public static void main(String args[]) {
int x;
int y;
x = 1;
y = x++;
System.out.print(y + "\n");
x = 1;
y = ++x;
System.out.print(y + "\n");
x = 1;
y = x--;
System.out.print(y + "\n");
x = 1;
y = --x;
System.out.print(y + "\n");
}
}
Program 2:
class incdem
{
public static void main(String args[]) {
int x;
int y;
x = 1;
x = x++;
System.out.print(x + "\n");
x = 1;
x = ++x;
System.out.print(x + "\n");
x = 1;
x = x--;
System.out.print(x + "\n");
x = 1;
x = --x;
System.out.print(x + "\n");
}
}
=> Both programms use the incrementor in the way: x = x ++
y = x ++
Result of both programms: 1 - 2 - 1 -
---------
program 3:
class incDe
{
public static void main(String args[]) {
int x;
int y;
x = 1;
x++;
System.out.print(x + "\n");
x = 1;
++x;
System.out.print(x + "\n");
x = 1;
x--;
System.out.print(x + "\n");
x = 1;
--x;
System.out.print(x + "\n");
}
}
Uses incrementor in the way: x++, x++ (instead of y = x++)
Result: 2 - 2 - 0 - 0
*************************************************
Why the different results between
x++
System.out.println(x)
and
y = x++
System.out.println(x)
???
******************************************+

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
the result of program 1+2 is
1-2-1-0
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
the increment is performed before the value is assigned if it is like this:
x = ++x;
otherwise, it performed after the value is assigned. so, when it says:
x = 1;
x = ++x;
x will be equal to 2 when it is printed. but, when it is like:
x = 1;
x = x++;
x is still equal to 1 because the increment was performed after the value was assigned.
same thing with y... and same thing with --.


what?
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Originally posted by Thomas Markl:
the result of program 1+2 is
1-2-1-0

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: incremetor