Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 845925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:31:57+00:00 2026-05-15T06:31:57+00:00

I am working on one project for performance enhancement. I had one doubt, while

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:31:58+00:00Added an answer on May 15, 2026 at 6:31 am

    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 ?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working a project tonight, I ended up using one .js resource file for
I am working on a project to process three files...two CSVs and one XML.
I am working on one project where there is a functionality need to implement
The worst thing when working on a one man project is the lack of
I am currently working on a project where one of our goals is to
I'm working on a project which contains several slave nodes and one master node.
I m working on project user to move the image in one position to
I am working on project where I have the checkInternet method in one of
I'm currently working on a project with a one page design that'll slide up
I'm working on problem 9 in Project Euler: There exists exactly one Pythagorean triplet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.