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 eg
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}]
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?
builder.append("firstName=" + firstName + ", ");
严格来说,它比Eclipse使用的表单效率低,因为它创建了一个中间String
,然后立即被丢弃。
除非我误解了这个问题,否则您可以在Eclipse首选项中更改模板:Java - >代码样式 - >代码模板。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.