JhinKazama
JhinKazama

Reputation: 55

Can I improve this method of concatenation of strings?

Here is the method :

    private String getCommentaireReprise(TableRow row) {

    return "Produit : " + getProduit(row) +
            "\n Dossier fournisseur : " + getDossierFournisseur(row) +
            "\n Dossier Truffaut : " + getDossierTruffaut(row) +
            "\n Ref loueur : " + getRefLoueur(row) +
            "\n Merchandising : " + getMerchandising(row) +
            "\n Libelle court : " + getLibelleCourt(row) +
            "\n Libelle SAP : " + getLibelleSap(row) +
            "\n Nombres Images 360 : " + getNombreImages(row) +
            "\n Uploadé par : " + getUploadePar(row) +
            "\n Crédit photo : " + getCreditPhoto(row);
}

Is is better to use a StringBuilder rather than this ? or to use JSONObject and to cast it at the end with .toString() ?

Upvotes: 0

Views: 80

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198023

The posted code actually performs better than code you'd manually write, even with a string builder, for any version of Java >= 9 -- due to the use of StringConcatFactory. StringConcatFactory can generate code better than you could possibly handwrite, since it can do tricks like directly writing into the final byte array used in the String instead of the copy that even StringBuilder has to do.

(Since you have tagged this question "performance" and "optimization," I assume pure speed is your priority. If it's about what's "better" in general, that would be an opinion-based question and would not be appropriate for StackOverflow as a result.)

Upvotes: 1

Related Questions