The String,stringbuffer and StringBuilder of the Java Core API and the difference and connection between equals and = =

Source: Internet
Author: User

String class:
Construction Method:

A:string () empty construct creates a string.
b:string (byte[] bytes) to convert a byte array into a string
C:string (byte[] bytes, int index, int length) Convert a portion of a byte array to a string
d:string (char[] value) to convert a character array to a string
e:string (char[) value, int index, int count) to turn a part of a character array into a string
f:string (string original) to convert a string to a string

Concatenation of strings:

string s = new string ();
 System.out.println ("s:" + s); The description overrides the ToString () method or it prints the address value
String s= "Hello";     s+= "World";  String stitching is very memory-intensive "Hello" "World" remains

Problem:
string S1 = new string ("Hello World"); String s2 = "Hello World" compares the differences.
For:
The former creates two objects, the latter creates only one object, and the next time the same content is created (in the same way) no longer generates new
String str = "Hello";
STR is a reference that points to the "Hello" object in a constant pool.
For the new String ("...")
The object new in the heap itself has a member Char []value to point to the string in the constant pool.

S1==S2//Return flase because the address value is compared for reference types
S1.equals (S2)//return True because string overrides the Equals method, it only compares values without comparing addresses
Memory diagram:

String S1 = "Hello";
String s2 = "Hello";
S1==S2; True their address is the same as
string s3 = new String ("Hello World");
String S4 = new string ("Hello World");
S3==S4//False their address is not the same as
String s5 = "Hello";
String s6 = "word";
String s7= "Helloword";
S7.equals (S5+S6);//true
//If the string variable is added, open the space first. Add and save
s7==s5+s6;//false
///If the constant is added, first add, then look in the constant pool, and if so, return the address of the constant pool, otherwise create a new space.
s7= "Hello" + "word";//false

One of the interview questions for string:
Why do I say string-type argument passing. Changes in formal parameters do not affect the content of the argument.
Because string is an immutable type, that is, the memory size from the moment it is declared is fixed and immutable, but StringBuffer can
String str = "java" + "is" + "I" + "life"; This sentence creates only one string object. Because the value of STR can be determined at compile time (those values are constant)

Search for strings

* int Length (): Gets the length of the string 
* char charAt (int index): Gets the character 
* int indexOf (int ch) at the specified index in the string: Gets the index of the first occurrence of this character of CH in this string. 
* int indexOf (string str): Gets the index in which STR is the first occurrence of this string. 
* int indexOf (int ch,int fromindex): Gets the index of the first occurrence of the CH character in this string, starting with the specified index. 
* int indexOf (string str,int fromindex): Gets the index of the first occurrence of the STR string in this string, starting with the specified index. 
* string substring (int start): Gets the substring. Intercept. From start to end. 

Optimizations when using strings: StringBuffer and StringBuilder
Much of the information says that priority is given to StringBuffer, because the data is jdk1.5 before it is obsolete.
In fact, we should give priority to using StringBuilder.
The only difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe, reduces efficiency, and takes precedence over StringBuilder when there is no multithreading.
Problem:
String, how does stringbuilder,stringbuffer differ?
String string length is fixed.
Stringbuilder/stringbuffer length is variable.
StringBuilder thread is unsafe and highly efficient
StringBuffer thread safety, low efficiency

StringBuffer: (Note two methods length () and capacity ());
You can get the length of the current stringbuffer by calling the length () method. The total allocation capacity can be obtained by invoking the capacity () method
Their general form is as follows:
int Length ()
int capacity ()
Example:

Class Stringbufferdemo
{
public static void Main (String args[])
{
StringBuffer sb = new StringBuffer ("Hello");

System.out.println ("buffer =" +SB);
System.out.println ("length =" +sb.length);
SYSTEM.OUT.PRINTLN ("capacity =" +sb.capacity);
}
}
Here is the output of this program, which shows how StringBuffer can reserve extra space for additional processing:
Buffer = Hello
Length = 5
capacity = 21

Because SB is initialized by the string "Hello" when it is created, it has a length of 5. Because the storage space is automatically increased for 16 additional characters, it has a storage capacity of 21.

Construction method:
stringbuffer ();  
StringBuffer (int capacity);
StringBuffer (String str);

Common methods:

Add character (how type):
append (Object obj);    Any character can be
added from the character
insert (int offset,object obj);
Delete function:
delect (int start,int end);      Deletes the data
delectcharat (int index) from start to end;    Deletes the data substitution function for the index position
:
replace (int start,int end,string str);

The return value of the above method is StringBuffer

Public String substring (int start);
Public String substring (int start, int end);   The return value is string
stringbuffer after using substring two methods, which itself does not change, returns a string type
example:
stringbuffer  sb = new StringBuffer ("Hello World");
Sb.substring (5);
System.out.println (SB);    The print remains Hello World
String s = sb.substring (5);
System.out.println (s);     Printing remains
the reverse function of the world stringbuffer: public
stringbuffer reverse ();

Conversion of StringBuffer and string into each other:

A:string->stringbuffer
Method 1:
String  s = "Hello World";
StringBuffer sb = new StringBuffer (s);
Method 2:
String  s = "Hello World";
StringBuffer sb = new StringBuffer ();
Sb.append (s);
A:stringbuffer->string
Method 1.
STRINGBUFFERSB = new StringBuffer ("Hello World");
string s = new string (SB);
Method 2.
STRINGBUFFERSB = new StringBuffer ("Hello World");
String s = sb.tostring ();

The difference between equals and = =:
int i=10;
int j=10;
At this time i==j;//true
String a = new string ("abc");
String b = new String ("abc");
At this time A==b;//false
The object variable is actually a reference to the memory address where the object is located, and the A and B strings use "new" so that there are two strings in memory that are "ABC". Since they are two, their natural memory addresses are different. The value of a and B is actually a value of two different memory addresses, so using the "= =" result is false. However, their content is "abc". should be "equal", but the "= =" operator does not design object content comparisons, because that's the equals thing. Let's see how the object Equals method is implemented:

Boolean equals (Object o) {return
  this==0;
}

The object class uses the "= =" operator by default. So if your class doesn't rewrite the Equals method and use "= =" to get the same result. So in the future, when deciding whether a variable is equal, do not take it for granted because it is determined by the creator of the class.

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.