FULL PRODUCT VERSION :
C:\Documents and Settings\Jon>java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
A DESCRIPTION OF THE PROBLEM :
Whilst source code #1 gives expected results, source code #2 and, source code #3 (which use pre-fix and post fix operators, provide misleading results when trying to carry out a mathematical operation.
Further information is available at http://introcs.cs.princeton.edu/java/43stack/Postfix.java.html.
ACTUAL -
Source code #1
233168
Source code #2
467
Source code #3
0
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
Source code #1 - correct
public class AddNumbers
{
public static void main (String[] args)
{
int result = 0;
for (int i = 0; i < 1000; i++)
{
if ((i % 3 == 0) || (i % 5 == 0)) result = result + i;
}
System.out.println(result);
}
Source code #2
public class AddNumbers
{
public static void main (String[] args)
{
int result = 0;
for (int i = 0; i < 1000; i++)
{
if ((i % 3 == 0) || (i % 5 == 0)) result = ++result;
}
System.out.println(result);
}
}
Source code #3
public class AddNumbers
{
public static void main (String[] args)
{
int result = 0;
for (int i = 0; i < 1000; i++)
{
if ((i % 3 == 0) || (i % 5 == 0)) result = result++;
}
System.out.println(result);
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
public class AddNumbers
{
public static void main (String[] args)
{
int result = 0;
for (int i = 0; i < 1000; i++)
{
if ((i % 3 == 0) || (i % 5 == 0)) result = result + i;
}
System.out.println(result);
}
C:\Documents and Settings\Jon>java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
A DESCRIPTION OF THE PROBLEM :
Whilst source code #1 gives expected results, source code #2 and, source code #3 (which use pre-fix and post fix operators, provide misleading results when trying to carry out a mathematical operation.
Further information is available at http://introcs.cs.princeton.edu/java/43stack/Postfix.java.html.
ACTUAL -
Source code #1
233168
Source code #2
467
Source code #3
0
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
Source code #1 - correct
public class AddNumbers
{
public static void main (String[] args)
{
int result = 0;
for (int i = 0; i < 1000; i++)
{
if ((i % 3 == 0) || (i % 5 == 0)) result = result + i;
}
System.out.println(result);
}
Source code #2
public class AddNumbers
{
public static void main (String[] args)
{
int result = 0;
for (int i = 0; i < 1000; i++)
{
if ((i % 3 == 0) || (i % 5 == 0)) result = ++result;
}
System.out.println(result);
}
}
Source code #3
public class AddNumbers
{
public static void main (String[] args)
{
int result = 0;
for (int i = 0; i < 1000; i++)
{
if ((i % 3 == 0) || (i % 5 == 0)) result = result++;
}
System.out.println(result);
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
public class AddNumbers
{
public static void main (String[] args)
{
int result = 0;
for (int i = 0; i < 1000; i++)
{
if ((i % 3 == 0) || (i % 5 == 0)) result = result + i;
}
System.out.println(result);
}
- relates to
-
JDK-7071601 clarify semantics of postfix increment/decrement
-
- Closed
-