J2ME Polish
J2ME Polish 2.4 Documentation
Enough Software

Java 5 Syntax Support

Java 5 introduced important syntatical changes that help to remain type safety and to increase productivity. Up to now this language improvements were unavailable to J2ME developers, since J2ME programs are limited to the Java 1.2 class format. Thanks to J2ME Polish you can now use generics, enums and foreach loops in your J2ME code as well! Java 5 Syntax Support for Java ME

Generics

Generics allow you to use collections in a typesafe manner. This means that the compiler ensures that your code only accesses types that you really meant to use in collections. With J2ME Polish you can use generics for both java.util collections like Vector or Hashtable as well as the de.enough.polish.util collections like ArrayList or HashMap:

this.storage = new RmsStorage<Vector<Note>>();
Vector<Note> vector = this.storage.read("notes");

You can use generics for any collection:

  • java.util.Vector
  • java.util.Stack
  • java.util.Hastable
  • de.enough.polish.util.ArrayList
  • de.enough.polish.io.RmsStorage

Enums

Enums are typesafe constants. In Java constants were typically implemented by having int constants like for example in the java.util.Calendar class:

int dayOfWeek = Calendar.getInstance().get( Calendar.DAY_OF_WEEK ); // Calendar.DAY_OF_WEEK is an int constant

Unfortunately int constants are not typesafe and are hard to understand. In the above example we get an int value for the current weekday, but does 0 mean Monday or Sunday? Or does the first weekday start with 1? Also the get() method accepts any int value and the compiler has no chance to verify that the given value is really supported by the method. If we provide a wrong value, only a runtime exception will occur later onwards.

Enums solve this problem by providing typesafe constants:

enum Weekday { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY };

Enums are types, so if you can have typesafe methods that really accept only a Weekday and not any other constant. Also return values do have a real meaning - it's for example much easier to understand Weekday.MONDAY than 0!

Autoboxing

Autoboxing saves you the trouble of casting when accessing collections or similar classes that you can parameterize with classes of primitives like Integer, Long or Boolean:

Vector<Integer> vector = new Vector<Integer>();
vector.add( 42 );
vector.add( 23 );
...
int value = vector.elementAt( 0 ); // = 42

Foreach Loops

Foreach loops allow you to easily traverse through collections and arrays. For iterating over an vector in J2ME, traditionally following code is required:

int size = vector.size();
for (int i = 0; i < size; i++) {
	Note note = (Note) vector.elementAt(i);
	//#style notesItem
	this.notesList.append(note.getText(), null);
}

Compare this with the much shorter and more up to the point Java 5 code:

for (Note note : vector) {
	//#style notesItem
	this.notesList.append(note.getText(), null);				
}

How to activate Java 5 syntax support

Just activate the "java5" postcompiler in your build.xml script for activating the Java 5 syntax support for your J2ME Polish projects, yes, it's that easy!

<j2mpolish>
	<deviceRequirements ../>
	<info ../>
	<build>
		<postcompiler name="java5" />
	</build>	
</j2mpolish>

You should also add the Java 5 syntax versions of our CLDC and J2ME Polish APIs to the classpath of your project. You find these packages in ${polish.home}/import/cldc-1.1-java5.jar and ${polish.home}/import/enough-j2mepolish-client-java5.jar. Don't forget to set the compiler compliance level of your project to Java 5.0 as well. In Eclipse you can do this by right-clicking your project > Properties > Java Build Path and Java Compiler.

Current Limitations

All Java 5 syntax elements are fully supported from J2ME Polish Beta 2 onwards. There are, however, some limitations in the current implementation:

TopicCurrent LimitationJ2ME Polish 2.0 Final
Generics HashMap implementation does not yet support generics. All collections, including all J2ME Polish collections support generics.
Enums Enums currently require a base class (de.enough.polish.java5.Enum) as well as a concrete implementation class. All enum references will be completely replaced with int constants, so that absolutely no overhead is produced.
Autoboxing Autoboxing currently requires the additional class de.enough.polish.java5.AutoBox. No additional class will be needed in the final release.
Foreach Foreach loops are currently not supported for de.enough.polish.util.ArrayList. Iterating through collections require the additional class de.enough.polish.util.IteratorUtil. Support for any list related collection will be implemented.

Java 5 Sample Application

Please refer to the sample application located in ${polish.home}/samples/java5 for an example how to code with Java 5 syntax within an J2ME environment.

back to top