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


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);
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.


Popular posts from this blog

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

Low Latency Microservices, A Retrospective

Unusual Java: StackTrace Extends Throwable