The Artima Developer Community
Sponsored Link

Java Buzz Forum
Code Inspection

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Wolf Paulus

Posts: 692
Nickname: wolfpaulus
Registered: Jan, 2004

Wolf Paulus is an experienced software developer focusing on Java, XML, Mac OS X, wireless/mobile ..
Code Inspection Posted: May 11, 2004 11:15 AM
Reply to this message Reply

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
Latest Java Buzz Posts
Latest Java Buzz Posts by Wolf Paulus
Latest Posts From Wolf's Web Journal

Advertisement
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.

FindFile.FindFileInClasspath( kPropFilePrefix + "*"+ kPropFileSuffix + kKeyProperties, vPropFiles );

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 final java.lang.String SUFFIX;

public Test(java.lang.String);
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."":()V
   4:   new     #2; //class StringBuffer
   7:   dup
   8:   invokespecial   #3; //Method java/lang/StringBuffer."":()V
   11:  ldc     #4; //String abc*123
   13:  invokevirtual   #5; //Method java/lang/StringBuffer.append:(Ljava/lang/String; 1000 )Ljava/lang/StringBuffer;
   16:  aload_1
   17:  invokevirtual   #5; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
   20:  invokevirtual   #6; //Method java/lang/StringBuffer.toString:()Ljava/lang/String;
   23:  astore_2
   24:  getstatic       #7; //Field java/lang/System.out:Ljava/io/PrintStream;
   27:  aload_2
   28:  invokevirtual   #8; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   31:  return


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.

Read: Code Inspection

Topic: "visualizing hypotheticals" Previous Topic   Next Topic Topic: [Apr 30, 2004 09:24 PDT] 20 Links

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use