Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toString in eclipse

The toString method was generated in eclipse as follows :

StringBuilder builder = new StringBuilder();
builder.append("Person [");
if (firstName != null)
    builder.append("firstName=").append(firstName).append(", ");
if (lastName != null)
    builder.append("lastName=").append(lastName).
if (title != null)
    builder.append("title=").append(title).append(", ");
if (car != null)
    builder.append("car=").append(car).append(", ");
    builder.append("]");

Why didn't they use more short form e.g.

builder.append("firstName=" + firstName + ", ");

Is it to save time with String creation?

How do I change template to this format ?

${object.className} [${member.name()}=${member.value}, ${otherMembers}]
like image 597
fastcodejava Avatar asked Feb 19 '26 21:02

fastcodejava


1 Answers

The first form is ever so slightly faster - in the other forms additional StringBuilder are created within the byte code. In general this does not matter much unless string creation is performed in a tight loop.

As for the second question, what have you tried?

like image 89
Maarten Bodewes Avatar answered Feb 25 '26 05:02

Maarten Bodewes