Performance Tip: Avoid "" + x to convert to a String.

Overview

Personally I find using "" +x to be clearer and shorter than using String.valueOf(x) Some believe the later is clearer. Certainly the later is faster for the CPU, but slower for the developer.

Avoid using "" + x when performance is critical

Using ""+x creates a StringBuilder which is redundant (this is two objects in one) Using String.valueOf(x) is more efficient. Using StringBuilder is never more efficient because it calls String.valueOf(x) anyway.

From LogManager.loadLoggerhandlers
System.err.println("" + ex);
ex.printStackTrace();
The use of "" + is not only inefficient, but pointless. This is because println(Object) will call String.valueOf() anyway. To make it doubly pointless the next line will do the same thing making the error and the message appear twice (even though it occurred once) The second line will also print the Stack Trace.

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?