[kaffe] Re: null string concatenation

Dalibor Topic robilad at kaffe.org
Tue Nov 25 05:35:02 PST 2003


Hi Ito,

thanks for taking the time to look into it.

Ito Kazumitsu wrote:
> Hi,
> 
> With reference to the bug:
>    http://www.kaffe.org/pipermail/kaffe/2003-November/044458.html
> 
> I suggest the following patch:
> 
> --- kopi-2.1B/src/kjc/JCompoundAssignmentExpression.java	2002-07-16 03:53:32.000000000 +0900
> +++ test/JCompoundAssignmentExpression.java	2003-11-25 19:26:58.000000000 +0900
> @@ -239,6 +239,13 @@
>  
>      if (oper == OPE_PLUS && type.equals(factory.createReferenceType(TypeFactory.RFT_STRING))) {
>        //      left.genCode(context, false);
> +      /* Isn't it stupid to call String.valueOf(String) ?
> +       * But we must do this because it may be null.
> +       */
> +      code.plantMethodRefInstruction(opc_invokestatic,
> +			       "java/lang/String",
> +			       "valueOf",
> +			       "(Ljava/lang/Object;)Ljava/lang/String;");
>        right.genCode(context, false);
>        if (!right.getType(factory).isReference()) {
>  	code.plantMethodRefInstruction(opc_invokestatic,
> 
> 
> By the way, the bytecode which JCompoundAssignmentExpression.java generates
> looks quite different from the bytecode produced by gcj or Sun's javac. The
> former uses String.concat and the latter uses StringBuffer for string
> concatenation.  Which is better?

I'd say that StringBuffer is better in general, if it can be used in 
multiple '+' invocations, without creating temporary Sting objects. You 
only need to allocate the buffer once per expression.

It may be possible to optimise it further by using a single StringBuffer 
per method employing '+' on Strings, and clearing it before each 
expression using + on Strings.

But using String.concat() is simpler, and leads to more understandable 
code ;)

cheers,
dalibor topic





More information about the kaffe mailing list