This post originated from an RSS feed registered with Java Buzz
by Wolf Paulus.
Original Post: Code Inspection
Feed Title: Wolf's Web Journal
Feed URL: http://wolfpaulus.com/feed/
Feed Description: Journal - dedicated to excellence, and motivated by enthusiasm to trying new things
I recently got the chance to participate in another code inspection, in which among other things, the following line of code was heavily criticized for using String concatenation on constants and not using a StringBuffer object for the string operation in general.
This is from a J2EE project and the code is used in a background thread and executes every couple of seconds.
Since I also do some work in the field of embedded software development, which usually involves memory, processing speed, and power constraints, I can really appreciate code level optimizations. However, this is not a MIDlet project nor does the code run all that often. Moreover, the concatenation really does add some clarity to what the code is doing and what the parameter's value is when the static method is called.
Now, to satisfy the optimization junkies, I have created the following small sample:
class Test {
public static final String PREFIX="abc";
public static final String SUFFIX="123";
public Test(String t) {
String s = Test.PREFIX + "*" + Test.SUFFIX + t;
System.out.println( s );
}
public static void main(String[] args) {
new Test(args[0]);
}
}
Compiling followed by disassembling shows the following code:
C:\temp\test>javac Test.java
C:\temp\test>javap -c Test
Compiled from "Test.java"
class Test extends java.lang.Object{
public static final java.lang.String PREFIX;
public static void main(java.lang.String[]);
Code:
0: new #9; //class Test
3: dup
4: aload_0
5: iconst_0
6: aaload
7: invokespecial #10; //Method "":(Ljava/lang/String;)V
10: pop
11: return
}
If you look at the disassembled code you will find out that the compiler combines the String constants into one and also uses a StringBuffer for the concatenation that needs to be done at runtime.