The difference between string class StringBuffer class and StringBuilder class in Java __java

Source: Internet
Author: User

First, the difference between the string class and the StringBuffer class is described, and the string class is a constant, cannot be added, and StringBuffer is a character buffer that can add strings to it. For example:

<span style= "FONT-SIZE:18PX;" >string str = "HelloWorld";

str = "Welcome";</span>

Here in fact the process is this: Generate a String Object "HelloWorld" quote held by STR, when executed by STR + + "welcome"; This line of code actually discards the original "HelloWorld" object and then creates a new Mosaic object, and then assigns the reference to Str. So a new string object is generated in the process, and then the original object is reclaimed by the garbage collector. must be to affect the performance of the program.


So, let's take a look at StringBuffer's approach now.


<span style= "FONT-SIZE:18PX;" >stringbuffer SB  = new StringBuffer ("HelloWorld");

Sb.append ("Welcome");</span>

The process of stringbuffer is like this. First, a StringBuffer object is constructed, in a buffer, so that the specified string sequence can be added directly in the buffer.


There is another way of writing that is certainly wrong:

StringBuffer sb = new StringBuffer ();

SB = "HelloWorld"; How can a string object be assigned to a StringBuffer object?

Then the following tests the performance test snippet for the string class and the StringBuffer class


The execution effect is not absolute, depending on the hardware configuration of each machine.

First, the performance of the String class

<span style= "FONT-SIZE:18PX;" >/* This code snippet tests the performance of the string class by   approximately 2854 milliseconds
		string tempvalue = "HelloWorld";
		
  		int times = 10000;
		
		String value = "";
		
		Long starttime = System.currenttimemillis ();
		
		for (int i = 0; I < times; i++)
		{
			value + = Tempvalue;
		}
		
		Long endtime = System.currenttimemillis ();
		
		System.out.println (endtime-starttime);
		
		*/</span>

And then the StringBuffer class.

<span style= "FONT-SIZE:18PX;" >/* This code snippet is used to test the performance of the StringBuffer class by   approximately 10 milliseconds
		String tempvalue = "HelloWorld";
		
		int times = 10000;
		
		StringBuffer sb = new StringBuffer ();
		
		Long starttime = System.currenttimemillis ();
		
		for (int i = 0; I < times; i++)
		{
			sb.append (tempvalue);
		}
		
		Long endtime = System.currenttimemillis ();
		
		System.out.println (endtime-starttime);
		
		*/</span>


In one is the comparison of StringBuilder and StringBuffer classes

How to say the StringBuilder class is the class that starts from the JDK5.0. Used to replace StringBuffer. However, thread safety is not guaranteed. If you need this synchronization, please use the StringBuffer class, StringBuilder class is thread unsafe. And StringBuffer is thread-safe.

StringBuilder is a little bit more performance than StringBuffer, but the two-class overall usage is almost the same.


This is a test snippet that tests the performance of the StringBuilder class.

<span style= "FONT-SIZE:18PX;" >/* This code snippet is used to test the performance of the StringBuilder class for  approximately 10 milliseconds
		
		String tempvalue = "HelloWorld";
		
		int times = 10000;
		
		StringBuilder sb = new StringBuilder ();
		
		Long StartTime  = System.currenttimemillis ();
		
		for (int i = 0; I < times; i++)
		{
			sb.append (tempvalue);
		}
		Long endtime = System.currenttimemillis ();
		
		System.out.println (endtime-starttime);
		*/</span>


To sum up: If you are using a constant to more, also use the String class, if you often change the value, please also use StringBuffer or StringBuilder.


In general performance StringBuilder > StringBuffer > String






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.