O
Oliver Wong
I've got two pieces of code; one of them compiles without error, and the
other one doesn't. I can't figure out why. Here's the code snippets:
<compilesFine>
import java.util.Vector;
public class HistoryObjectsManager {
public void doSomethingWithRecord(Record record) {
for (Integer integer : record.getVector()) {
//Does nothing.
}
}
}
abstract class Record {
public Vector<Integer> getVector() {
return new Vector<Integer>();
}
}
</compilesFine>
<generatesCompileError>
import java.util.Vector;
public class HistoryObjectsManager {
public void doSomethingWithRecord(Record record) {
for (Integer integer : record.getVector()) {
//Does nothing.
}
}
}
abstract class Record<A> {
public Vector<Integer> getVector() {
return new Vector<Integer>();
}
}
</generatesCompileError>
The only difference between the two is that Record has a type parameter.
The compile error I get is:
<errorMessage>
HistoryObjectsManager.java:5: incompatible types
found : java.lang.Object
required: java.lang.Integer
for (Integer integer : record.getVector()) {
^
</errorMessage>
I'm using JavaC 1.5.0.
Is this a bug in the compiler or a misunderstanding on my part about
generics?
- Oliver
other one doesn't. I can't figure out why. Here's the code snippets:
<compilesFine>
import java.util.Vector;
public class HistoryObjectsManager {
public void doSomethingWithRecord(Record record) {
for (Integer integer : record.getVector()) {
//Does nothing.
}
}
}
abstract class Record {
public Vector<Integer> getVector() {
return new Vector<Integer>();
}
}
</compilesFine>
<generatesCompileError>
import java.util.Vector;
public class HistoryObjectsManager {
public void doSomethingWithRecord(Record record) {
for (Integer integer : record.getVector()) {
//Does nothing.
}
}
}
abstract class Record<A> {
public Vector<Integer> getVector() {
return new Vector<Integer>();
}
}
</generatesCompileError>
The only difference between the two is that Record has a type parameter.
The compile error I get is:
<errorMessage>
HistoryObjectsManager.java:5: incompatible types
found : java.lang.Object
required: java.lang.Integer
for (Integer integer : record.getVector()) {
^
</errorMessage>
I'm using JavaC 1.5.0.
Is this a bug in the compiler or a misunderstanding on my part about
generics?
- Oliver