The difference between int and integer,string,stringbuffer,stringbuilder in Java

Source: Internet
Author: User
Tags stringbuffer
int and integer differences in Java

int is the basic type, direct deposit value

An integer is an object that points to this object with a reference

The int and integer relationships in Java are more subtle. The relationship is as follows:

1.int is the basic data type;

2.Integer is the encapsulated class of int;

Both 3.int and integer can represent a certain number;

4.int and integer cannot be used for each other because of their two different data types;

Why to provide packing class ...
one is to convert among various types, through various methods of invocation. Otherwise you can't transform directly through variables.
for example, the int is now converted to string
int a=0;
String result=integer.tostring (a);
In Java, packaging classes, more use is used in a variety of data types of transformation.
the difference between the String,stringbuffer,stringbuilder

String string Constants
StringBuffer string variable (thread safe)
StringBuilder string variable (not thread safe)
execution Efficiency: StringBuilder > StringBuffer > String



briefly, the main performance difference between a string type and a StringBuffer type is that a string is an immutable object, so each time a change is made to the string type, it is equivalent to generating a new string object, and then pointing the pointer to the new S Tring objects, so it is best not to use string to change the content, because each generation object will have an impact on system performance, especially when there are no more references in memory, the JVM's GC will start to work, and that speed is bound to be quite slow.

1. String class

The value of string is immutable, which causes a new string object to be generated every time a string is created, not only inefficient, but also a waste of limited memory space.
String a = "a"; Suppose a points to address 0x0001
A = "B";//After 0x0002, a points to address, but the "a" saved in the 0x0001 address still exists, but is no longer pointed to by a, and a has already pointed to another address.
Therefore, the operation of string is to change the assignment address instead of changing the value operation.

2. StringBuffer is a variable class, and thread-safe string manipulation class, and any action on the string it points to does not produce a new object. Each StringBuffer object has a buffer capacity that does not allocate new capacity when the string size does not exceed capacity, and automatically increases capacity when the string size exceeds capacity.

StringBuffer buf=new StringBuffer (); Allocate a character buffer of 16 bytes long
StringBuffer buf=new StringBuffer (512); Allocate a character buffer of 512 bytes long
StringBuffer buf=new StringBuffer ("This is a test")//holds a string in the buffer and reserves a 16-byte empty buffer at the back.

3.StringBuffer
The StringBuffer and StringBuilder functions are basically similar in that the main difference is that the StringBuffer class approach is multithreaded and secure, while StringBuilder is not thread-safe, compared to The StringBuilder class will be slightly faster. For strings that often change values, you should use the StringBuffer and StringBuilder classes.

4. Thread Safety
StringBuffer Thread Safety
StringBuilder thread is not secure

5. Speed
In general, speed from fast to slow: stringbuilder>stringbuffer>string, this comparison is relative, not absolute.

6. Summary
(1). If you want to manipulate a small amount of data with = String
(2). Manipulating large amounts of data under a single threaded operation string buffer = StringBuilder
(3). Manipulate a large amount of data under a multithreaded operation string buffer = StringBuffer

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.