tags:

views:

62

answers:

4

I have a java 1.5 compiler, and I'd prefer not to also have a 1.4 compiler, but I need to use it because, apart from string concatenation, everything built using the 1.5 compiler works in a 1.4 environment. The only difference is between StringBuilder and StringBuffer. Is there a way to make the 1.5 compiler use the 1.4 string concatenation class?

A: 

Can't you just use StringBuffer in your code? That way, it should run in either environment .. no special magic required. StringBuilder is just an unsynchronized version of StringBuffer. StringBuilder is only available in Java 1.5, so as long as you avoid it and use StringBuffer instead, it should run in either environment.

JasonStoltz
Not if I'm using "A" + "B"
Ed Marty
@Ed Marty OK, I understand what you're asking.
JasonStoltz
+3  A: 

You can use the regular string concatenation (like "A" + "B") and while compiling the source, indicate the platform for which the .class files need to be generated using the javac's "-target" option.

Pangea
`-source` as well or it wont allow you `-target`. You should also use `-bootclasspath` to use the old `rt.jar` - not just because you might accidentally use a "new" API, but the same source can use overloads that weren't present in the old Java library.
Tom Hawtin - tackline
A: 

Whenever you use the "+" dealing with a String you use the string concatenation class, which can be inefficient. StringBuilder is faster, and you can use its .append("text") method after creating a new StringBuilder object to use. It's actually preferred for single-thread use in Java 1.5+, as StringBuffer performs synchronization and is only recommended for multiple threads.

Is there a specific reason you can't use StringBuilder?

rownage
+1  A: 

Looks like all you want to do is tell the 1.5 compiler to compile for 1.4.

javac -target 1.4

Tom