-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
7
-
x86
-
windows_vista
FULL PRODUCT VERSION :
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b71)
Java HotSpot(TM) Client VM (build 16.0-b08, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.0.6002]
A DESCRIPTION OF THE PROBLEM :
When creating or nulling reference types using enhanced for loop it silently fails. It understandably behaves the way it does, however it should be a compiler warning or error (C# treats this as a compilation error).
Look at the test code below and notice the different behavior between the two (semantically) similar for loops.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See sample code below.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compiler warning or compiler error.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class ForEach
{
public static Object[] initializeArray(Object[] objects)
{
for ( int i = 0; i < objects.length; i++ )
{
objects[i] = new Object();
}
return objects;
}
public static Object[] initializeArrayEnhanced(Object[] objects)
{
for (Object obj : objects )
{
obj = new Object();
}
return objects;
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class ForEachTest
{
@Test
public void testInitializeArray()
{
Object[] objects = new Object[5];
Object[] result = ForEach.initializeArray(objects);
assertNotNull(result);
for ( int i = 0; i < result.length; i++ )
{
assertNotNull(result[i]);
}
}
@Test
public void testInitializeArrayEnhanced()
{
Object[] objects = new Object[5];
Object[] result = ForEach.initializeArrayEnhanced(objects);
assertNotNull(result);
for ( int i = 0; i < result.length; i++ )
{
assertNotNull(result[i]);
}
}
}
---------- END SOURCE ----------
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b71)
Java HotSpot(TM) Client VM (build 16.0-b08, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.0.6002]
A DESCRIPTION OF THE PROBLEM :
When creating or nulling reference types using enhanced for loop it silently fails. It understandably behaves the way it does, however it should be a compiler warning or error (C# treats this as a compilation error).
Look at the test code below and notice the different behavior between the two (semantically) similar for loops.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See sample code below.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compiler warning or compiler error.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class ForEach
{
public static Object[] initializeArray(Object[] objects)
{
for ( int i = 0; i < objects.length; i++ )
{
objects[i] = new Object();
}
return objects;
}
public static Object[] initializeArrayEnhanced(Object[] objects)
{
for (Object obj : objects )
{
obj = new Object();
}
return objects;
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class ForEachTest
{
@Test
public void testInitializeArray()
{
Object[] objects = new Object[5];
Object[] result = ForEach.initializeArray(objects);
assertNotNull(result);
for ( int i = 0; i < result.length; i++ )
{
assertNotNull(result[i]);
}
}
@Test
public void testInitializeArrayEnhanced()
{
Object[] objects = new Object[5];
Object[] result = ForEach.initializeArrayEnhanced(objects);
assertNotNull(result);
for ( int i = 0; i < result.length; i++ )
{
assertNotNull(result[i]);
}
}
}
---------- END SOURCE ----------