Can I specify what class to use for string concatenation when using a java compiler?
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?
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.
Looks like all you want to do is tell the 1.5 compiler to compile for 1.4.
javac -target 1.4
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.
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?
精彩评论