New Collections in 1.5 question

what would happen trying to run some code compiled in 1.5 that used the new ‘type checking’ style collections classes in java 1.4 VM?

ie. Java 1.4:
ArrayList oldList = new ArrayList();
oldList.add(“Hello”);
oldList.add(“there”);

Iterator i = oldList.iterator();
while(i.hasNext())
{
System.out.println((String)(i.next())
}

Java 1.5:
ArrayList newList = new ArrayList();
newList.add(“Hello”);
newList.add(“there”);

Iterator i = newList.iterator();
while(i.hasNext())
{
System.out.println(i.next())
}

seems to me that all it’s doing is making sure that you are only putting in a specific type at compile-time, which means you don’t have to cast it when you want to use it.

The 1.5 compiler does 3 things here: 1. it complains if you break the new rules 2. it writes normal JDK1.4 code but inserts casts to String behind the scenes for you 3. it uses a newer classfile version number so it won’t work on 1.4 anyway :slight_smile: You have to compile with -target 1.4 to get it to run.

Cas :slight_smile: