Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which toString() method can be used performance wise?

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.

like image 321
M.J. Avatar asked Jun 16 '10 05:06

M.J.


People also ask

What is to toString () method for?

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

What type of method is toString ()?

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.

What is the purpose of toString () and equals () methods in Java class?

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.

What does the object toString () method do by default?

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.


1 Answers

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.

Related questions

  • StringBuilder vs String concatenation in toString() in Java
  • String, StringBuffer, and StringBuilder
  • StringBuilder and StringBuffer in Java

A String.format option

One 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.

API links

  • java.util.Formatter syntax

On Class Literals

I've corrected a syntax error in the original code from this.class (which doesn't compile) to this.getClass().

See also

  • JLS 15.8.2 Class Literals

Related questions

  • What is a class literal in Java ?
like image 119
polygenelubricants Avatar answered Oct 01 '22 15:10

polygenelubricants