Unit tests for Java string concatenation

Source: Internet
Author: User

Overall content Introduction

This article is divided into several parts:
* Comparison of String, StringBuffer, StringBuilder
* Comparison of string stitching methods
What to use:
* JUNIT4 Unit Test
* SLF4J's log frame

Comparison of String, StringBuffer, StringBuilder

The scenarios used by these three are summarized as follows (see: "Writing a quality code: 151 recommendations for improving Java programs"):

    1. String: String classes can be used in scenes where the strings do not change frequently, such as declarations of constants, small amounts of variable operations, and so on.

    2. StringBuffer: In frequent string operations (stitching, replacing, deleting, etc.) and running in multithreaded environments, you might consider using StringBuffer, such as XML parsing, HTTP parameter parsing, and encapsulation.

    3. StringBuilder: In frequent string operations (stitching, replacing, deleting, etc.) and running in multi-threaded environments, you might consider using StringBuffer, such as SQL statement Assembly, JSON encapsulation, and so on (it seems like these two I also use | StringBuffer).

string concatenation method Comparison
 PackageCom.zy.util;ImportOrg.junit.Test;ImportOrg.slf4j.Logger;ImportOrg.slf4j.LoggerFactory; Public  class teststring {    Private FinalLogger Logger = Loggerfactory.getlogger ( This. GetClass ());@Test     Public void Testplus() {String s ="";Longts = System.currenttimemillis (); for(inti =0; I <10000;        i++) {s = s + string.valueof (i); }LongTe = System.currenttimemillis ();//system.out.println ("+ Cost {} MS:" + (Te-ts));Logger.info ("+ Cost {} MS", te-ts); }@Test     Public void Testconcat() {String s ="";Longts = System.currenttimemillis (); for(inti =0; I <10000;        i++) {s = S.concat (string.valueof (i)); }LongTe = System.currenttimemillis ();//system.out.println ("concat Cost {} MS:" + (Te-ts));Logger.info ("concat Cost {} MS", te-ts); }@Test     Public void Teststringbuffer() {StringBuffer SB =NewStringBuffer ();Longts = System.currenttimemillis (); for(inti =0; I <10000;        i++) {sb.append (string.valueof (i)); } sb.tostring ();LongTe = System.currenttimemillis ();//system.out.println ("StringBuffer Cost {} MS:" + (Te-ts));Logger.info ("StringBuffer Cost {} MS", te-ts); }@Test     Public void Teststringbuilder() {StringBuilder SB =NewStringBuilder ();Longts = System.currenttimemillis (); for(inti =0; I <10000;        i++) {sb.append (string.valueof (i)); } sb.tostring ();LongTe = System.currenttimemillis ();//system.out.println ("StringBuilder Cost {} MS:" + (Te-ts));Logger.info ("StringBuilder Cost {} MS", te-ts); }}

The results of the above procedure are as follows:

 --6-9  -: on: - com. Zy. Util. TestStringTestplus Information: + Cost504Ms --6-9  -: on: - com. Zy. Util. TestStringTestconcat Information: concat cost196Ms --6-9  -: on: - com. Zy. Util. TestStringTeststringbuffer Information: StringBuffer cost2Ms --6-9  -: on: - com. Zy. Util. TestStringTeststringbuilder Information: StringBuilder cost2Ms

As you can see, the + and concat are slower and need to create a new string.
and StringBuffer and StringBuilder faster.

Reference Resources
    • JUnit Unit Test: MU Lesson Network-Unit Test
    • SLF4J Log Frame: Download introduction
    • Reference Blog: 1 2 3
Insufficient place
    • Use JAVAP to decompile, and then do the analysis

Unit tests for Java string concatenation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.