I have a problem with BlackBerry JDE 6.0 The following code give me the warning (and cause error at runtime)
String s = "hello";
doSomeStuff(s + "world");
The warning:
Warning!: Reference to undefined class: java.lang.StringBuilder
I don’t use any StringBuilder. I have searched and found The most recent version of RIM API does not contain StringBuilder class.
Changing JRE version into 1.4 can help, but it gave me a big trouble because I couldn’t use generic collection and some new apis in this version.
Another solution is I can use StringBuffer, but can’t I simply use ‘+’ operator? Why is it hard to try?
Update:
I looking for another way to use ‘+’ operator, because my code has used many of them and I don’t want to spend many time to replace all of them.
The Java compiler will automatically convert any expression with a series of string concatenations to use a buffer. Before Java 1.5, there was only one choice – StringBuffer. However, it suffered from the convention in early Java of synchronizing all public methods. In Java 1.5 a new buffer class was added – StringBuilder – which is better because it drops synchronization, leaving it up to users of the class to properly synchronize access. When the Java compiler is targeting Java 1.5 or later, it will use StringBuilder. For pre-1.5, it will use StringBuffer.
BlackBerry devices use Java-ME, which is based on Java 1.3, so no StringBuilder class is present. Your problem is that you are writing modern Java-SE code and expecting to deploy it on a Java-ME BlackBerry device. If you are using Eclipse, change your Java language compliance level to 1.3. This will make the compiler properly produce StringBuffer references. It will also make use of generics a syntax error. This is expected for BlackBerry development – you don’t get generics.
Example code:
Bytecode result when compiled with
javac -source 1.5 -target 1.5 test.javaBytecode result when compiled with
javac -source 1.3 -target 1.3 test.java