I am working on one project for performance enhancement. I had one doubt, while we are during a process, we tend to trace the current state of the DTO and entity used. So, for this we have included toString() method in all POJOs for the same. I have now implemented toString() in three different ways which are following :-
public String toString() {
return "POJO :" + this.class.getName() + " RollNo :" + this.rollNo + " Name :" + this.name;
}
public String toString() {
StringBuffer buff = new StringBuffer("POJO :").append(this.class.getName()).append(" RollNo :").append(this.rollNo).append(" Name :").append(this.name);
return buff.toString();
}
public String toString() {
StringBuilder builder = new StringBuilder("POJO :").append(this.class.getName()).append(" RollNo :").append(this.rollNo).append(" Name :").append(this.name);
return builder .toString();
}
can anyone please help me to find out which one is best and should be used for enhancing performance.
The toString() method Typically, it is used to construct a string containing the information on an object that can be printed, and we can redefine it for a certain class. If we do not redefine it, the toString() method of the class Object is used (which prints a system code for the object).
A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.
The toString method is used to return the String representation of an object (converting an object to a String). Like equals , all Java objects have a toString method. However, also like equals not all classes provide particularly useful implementations.
By default the toString() method will return a string that lists the name of the class followed by an @ sign and then a hexadecimal representation of the memory location the instantiated object has been assigned to.
The one with the +
is fine in this case. It's more readable, and it's just as performant compared to the StringBuilder/StringBuffer
version, since it' doesn't happen inside a loop.
If you are building a String
inside a loop, then more often than not you should use StringBuilder
. Only use StringBuffer
if you need its synchronized
feature, which doesn't happen very often.
Simplistically speaking (not true always, but is a good rule of thumb), unless you're doing a +=
with a String
, you don't really need a StringBuilder/StringBuffer
.
String.format
optionOne option often not considered is to use String.format
. It'll look something like this:
return String.format("POJO : %s RollNo %s : Name : %s",
this.getClass().getName(),
this.rollNo,
this.name
);
I find that this is the most readable and maintainable version.
Is this faster? Maybe yes, maybe not. It usually doesn't matter for common use case scenarios for something like toString()
. Strive for readability, only optimize if profiling says it's necessary.
java.util.Formatter
syntaxI've corrected a syntax error in the original code from this.class
(which doesn't compile) to this.getClass()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With