OnJava8-Examples/holding/ApplesAndOrangesWithoutGenerics.java

29 lines
757 B
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: holding/ApplesAndOrangesWithoutGenerics.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-06-03 11:41:10 -07:00
// Simple container use (suppressing compiler warnings)
2015-04-20 15:36:01 -07:00
// {ThrowsException}
import java.util.*;
class Apple {
private static long counter;
private final long id = counter++;
public long id() { return id; }
}
2015-05-18 23:05:20 -07:00
class Orange {}
2015-04-20 15:36:01 -07:00
public class ApplesAndOrangesWithoutGenerics {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ArrayList apples = new ArrayList();
for(int i = 0; i < 3; i++)
apples.add(new Apple());
// Not prevented from adding an Orange to apples:
apples.add(new Orange());
2015-05-18 23:05:20 -07:00
for(Object apple : apples) {
2015-05-05 14:05:39 -07:00
((Apple) apple).id();
2015-04-20 15:36:01 -07:00
// Orange is detected only at run time
2015-05-05 14:05:39 -07:00
}
2015-04-20 15:36:01 -07:00
}
2015-06-03 11:41:10 -07:00
} ///:~