Performance Tip: Avoid creating an Object to convert between a primitive and String.

Overview

One way of parsing a String or converting to a String is to create the wrapper object. This is often redundant.

Avoid creating an object to parse a primitive

From ExsltMath.constant()
return new Double(value).doubleValue();
All the wrapper class have parseXxxx() methods which take a String and give you a primitive without creating an extra object.
return Double.parseDouble(value);

Avoid creating a Wrapper to convert to a String


From CMAny.toString()
strRet.append
(
    " (Pos:"
    + new Integer(fPosition).toString()
    + ")"
);
The creation of the new Integer is redundant. String.valueOf(int) can be used.
strRet.append
(
    " (Pos:"
    + String.valueOf(fPosition)
    + ")"
);
In this situation, if we don't use String.valueOf(), it will be called anyway.
strRet.append
(
    " (Pos:"
    + fPosition
    + ")"
);
Using String + with StringBuilder.append() can be made more efficient.
strRet.append(" (Pos:").append(fPosition).append(")");

Comments

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

What does Chronicle Software do?