String and StringBuffer Parsing

Source: Internet
Author: User

Question 1: What have I declared!
String s = "Hello world! ";
Many people have done such a thing, but what have we declared? The answer is usually a String with the content "Hello world !". Fuzzy answers are usually the root cause of unclear concepts. If you want to answer the question accurately, half of them will probably make a wrong answer.
This statement declares a reference pointing to an object named "s". It can point to any object of the String type and currently points to "Hello world! "This String type object. This is what actually happens. We didn't declare a String object. We just declared a reference variable that can only point to the String object. Therefore, if you run the following statement:
String string = s; www.2cto.com
We declared another reference that can only point to the String object, named string, and no second object is generated. The string still points to the original object, that is, the same object as s.
 
Question 2: What is the difference between "=" and equals?
= The operator is used to compare whether the values of variables are equal. A better understanding is:
Int a = 10;
Int B = 10;
Then a = B will be true.
But what is hard to understand is:
String a = new String ("foo ");
String B = new String ("foo ");
If a = B, false is returned.
According to the previous post, object variables are actually a reference. Their values point to the memory address of the object, not the object itself. Both a and B use the new operator, which means that two strings with content "foo" will be generated in the memory. Since they are "two" strings, they are naturally located at different memory addresses. The values of a and B are actually two different memory address values. Therefore, the result is false when the "=" operator is used. It is true that the content of objects a and B is "foo" and should be "equal", but the = operator does not involve the comparison of object content.
The comparison of object content is exactly what the equals method does.
Let's take a look at how the equals method of the Object is implemented:
Boolean equals (Object o ){
Return this = o;
}
The = Operator is used by default for Object objects. Therefore, if your own class does not overwrite the equals method, you will get the same result when using equals and =. We can also see that the equals method of the Object does not meet the goal that the equals method should achieve: compare whether the content of the two objects is equal. Because the answer should be determined by the class creator, the Object leaves this task to the class creator.
Take a look at the next extreme class:
Class Monster {
Private String content;
...
Boolean equals (Object another ){
Return true;
}
}
I overwrite the equals method. This implementation will always return true regardless of the content of the Monster instance.
Therefore, when you use the equals method to determine whether the object content is equal, do not take it for granted. Because you may think they are equal, and the authors of this class do not think so, and the implementation of the equals method of the class is controlled by him. If you need to use the equals method or any hash code-based set (HashSet, HashMap, HashTable ), check the java doc to see how the equals logic of this class is implemented.
 
Question 3: Have the String changed?
No. Because String is designed as an immutable class, all its objects are immutable objects. See the following code:
String s = "Hello ";
S = s + "world! ";
Is the object pointed to by s changed? It is easy to draw this conclusion from the first article in this series. Let's see what happened. In this Code, s originally points to a String object with the content "Hello". Then we perform the + operation on s. Does the object s points to change? The answer is no. At this time, s points to another String object instead of the original object, and the content is "Hello world! ", The original object still exists in the memory, but the reference variable s no longer points to it.
Through the above description, we can easily draw another conclusion, if you often make various modifications to the string, or make unforeseen modifications, therefore, using String to represent strings will cause a large memory overhead. Because the String object cannot be changed after it is created, a String object is required for each different String. In this case, you should consider using the StringBuffer class, which allows modification, instead of generating a new object for each different string. In addition, the conversion between the two types of objects is very easy.
At the same time, we can also know that if you want to use a String with the same content, you do not have to create a new String each time. For example, we need to initialize a String reference variable named s in the constructor and set it as the initial value. We should do this:
Public class Demo {
Private String s;
...
Public Demo {
S = "Initial Value ";
}
...
}
Rather
S = new String ("Initial Value ");
The latter calls the constructor every time to generate a new object, which has low performance and high memory overhead and has no significance. Because the String object cannot be changed, for strings with the same content, it can be expressed as long as a String object. That is to say, the constructor above is called multiple times to create multiple objects, and their String type attribute s points to the same object.
The above conclusion is based on the fact that for String constants, if the content is the same, Java considers them to represent the same String object. Using the new keyword to call the constructor always creates a new object, regardless of whether the content is the same or not.
The reason for designing a String class as an immutable class is determined by its purpose. In fact, classes in many Java standard library are not variable. When developing a system, we sometimes need to design immutable classes to pass a set of related values, which is also the embodiment of Object-oriented thinking. The immutable class has some advantages. For example, because its object is read-only, concurrent multi-thread access will not have any problems. Of course, there are also some shortcomings. For example, each State must be represented by an object, which may cause performance problems. Therefore, the Java standard library also provides a variable version, namely, StringBuffer.
 
Let's take a look at a strange program:
Public class TestString {
Public static void main (String [] args ){
String s1 = "Monday ";
String s2 = "Monday ";
}
}
This program is really simple! But what's the problem?
1. worries from String
In the above section, how many objects are there?
Many people may blurted out: Two, s1 and s2.
Why?
String is a final class, and its value is unchangeable.
It seems quite reasonable. Let's check it and change the program slightly.
You can see the result:
Public class TestString {
Public static void main (String [] args ){
String s1 = "Monday ";
String s2 = "Monday ";
If (s1 = s2)
System. out. println ("s1 = s2 ");
Else
System. out. println ("s1! = S2 ");
}
}
A lot of people will say there are more than two objects.
Compile and run the program. Output: s1 = s2
Ah! Why s1 = s2?
= It clearly means that s1 and s2 reference the same String object -- "Monday "!
2. ever-changing String
If you change the program a little bit, you may find it strange:
Public class TestString {
Public static void main (String [] args ){
String s1 = "Monday ";
String s2 = new String ("Monday ");
If (s1 = s2)
System. out. println ("s1 = s2 ");
Else
System. out. println ("s1! = S2 ");
If (s1.equals (s2 ))
System. out. println ("s1 equals s2 ");
Else
System. out. println ("s1 not equals s ");
}
}
We will create s2 with the new operator
Program output:
S1! = S2
S1 equals s2
Well, obviously.
S1 s2 references two "Monday" String objects respectively.
But why are the two procedures different?
 
3. Swimming in the String Swimming Pool
It turns out that a string buffer pool will be created when the program is running. When the expression s2 = "Monday" is used to create a string, the program first searches for objects with the same value in the String buffer pool. In the first program, s1 is first placed in the pool. Therefore, when s2 is created, the program finds s1 with the same value and uses s2 to reference the object "Monday" referenced by s1. In the second stage, the program uses the new operator, which clearly tells the program: "I want a new one! Don't be old !" And is a new "Monday" Sting object created in the memory. They have the same value but different positions. One is swimming in the pool and the other is resting on the shore. Oh, it's a waste of resources. What should we do separately?
 
4. Continue diving
Change the program again:
Public class TestString {
Public static void main (String [] args ){
String s1 = "Monday ";
String s2 = new String ("Monday ");
S2 = s2.intern ();
If (s1 = s2)
System. out. println ("s1 = s2 ");
Else
System. out. println ("s1! = S2 ");
If (s1.equals (s2 ))
System. out. println ("s1 equals s2 ");
Else
System. out. println ("s1 not equals s2 ");
}
}
Join this time: s2 = s2.intern ();
Wow! Program output:
S1 = s2
S1 equals s2
It turns out that after the s2 program is created, intern () is used to flip it into the pool.
Haha, This Time s2 and s1 have referenced the same object. We have successfully reduced the memory usage.
 
5. = battle with equals ()
String is an object. To compare whether the values of two different String objects are the same, it is obvious that the equals () method is used. However, if there are so many String objects in the program, equals is used for so many times. Oh, my God, it's really slow.
Better solution:
Intern () All strings to the buffer pool. It is best to perform this operation when new is used.
String s2 = new String ("Monday"). intern ();
Well, are you all soaking in the pool? Haha
Now I can use = to compare the value of a String object with no scruples.
It's so refreshing, fast, and convenient!
 
1. Review the bad-tempered String old brother
Example 1:
Class Str {
Public static void main (String [] args ){
String s = "Hi! ";
String t = "Hi! ";
If (s = t)
System. out. println ("equals ");
Else
System. out. println ("not equals ");
}
}
What does the program output?
Program output: equals
 
2. Oh, my God, it's mixing up again.
Example 2:
Class Str {
Public static void main (String [] args ){
String s = "HELLO ";
String t = s. toUpperCase ();
If (s = t)
System. out. println ("equals ");
Else
System. out. println ("not equals ");
}
}
So what does this program output?
Be careful! Be careful! Don't be confused by the character String!
It outputs: equals
 
Simply change the program:
Class Str2 {
Public static void main (String [] args ){
String s = "Hello ";
String t = s. toUpperCase ();
If (s = t)
System. out. println ("equals ");
Else
System. out. println ("not equals ");
}
}
You may say, isn't it?
No! The difference is true! Output this time: not equals
 
3. Do you know your horse?
"To tame the wild horse, you need to understand its nature," said the cowboy.
Do you know about String?
To interpret the String API, you can see:
The toUpperCase () and toLowerCase () Methods return a New String object. The original String indicates the upper or lower case of the String. However, note the following: if the original string is in upper or lower case, the original object is returned.
This is why s and t are unclear in the second program.
There seems to be no better way to treat this naughty String that has never been changed.
Let's dissect it and see what structure it has:
(1) charAt (int n) returns the n-Position Character in the string. The first character is 0,
The last character is length ()-1. A large brick is thrown at a wrong position:
StringIndexOutOfBoundsException is really big enough
(2) concat (String str) connects a str after the original object, but returns a New String object.
(3) Revoke signorecase (String str) case-insensitive equals Method
The essence of this method is to call the static character method toUpperCase () or toLowerCase (), convert the two characters in comparison, and then perform the = operation.
(4) trim () returns a new object, which removes the spaces at the beginning and end of the original object. Similarly, if the result is no different from the original object, the original object is returned.
(5) Does the toString () String class also have the toString () method?
This is really an interesting question, but without it, your String object may not be used in System. out. println ().
Be careful, it returns the object itself
There are many other methods in the String class, which will bring a lot of convenience to you.
There will also be a lot of confusion, So sticking to principles is the most critical
 
4. I want to buy a better horse.
Let's buy StringBuffer, the younger brother with a more gentle String.
At this time, some people will disagree: it is very useful, and it is very efficient. How can it be a younger brother?
It is very simple. It has fewer interaction functions than String. If you want to edit a String, it is not convenient. You will be disappointed with it, but it does not mean it is not powerful.
Public final class String implements Serializable, Comparable, CharSequence
Public final class StringBuffer implements Serializable, CharSequence
Obviously, the younger brother has fewer things, but this will not interfere with its future.
StringBuffer is not inherited by String, but it must be noted that it is also final, which is the same root
Let's look at his method. So many stable and reliable methods are used more efficiently than naughty strings.
Java provides an independent StringBuffer class for string objects to be changed.
Its instance is immutable (final). The reason why they need to be separated is that the string modification requires the system to increase sales volume and occupy more space, it is believed that when 10000 people are swimming in a small swimming pool and another 10000 people are waiting to enter the swimming pool, and 10000 people are anxious to get angry and watching the excitement next to them, the administrator of your String swimming pool will also be overwhelmed.
Without changing the String, a simple String class is enough for you to call. To change the content of a String frequently, you must use the StringBuffer to support the ship.
 
5. Support Delivery
(1) length () and capacity ()
The length () in the String returns the length of the String. The same is true for the sibling StringBuffer. Are they determined by the length of the characters contained in the object capacity?
Public class TestCapacity {
Public static void main (String [] args ){
StringBuffer buf = new StringBuffer ("it was the age of wisdom ,");
System. out. println ("buf =" + buf );
System. out. println ("buf. length () =" + buf. length ());
System. out. println ("buf. capacity () =" + buf. capacity ());
String str = buf. toString ();
System. out. println ("str =" + str );
System. out. println ("str. length () =" + str. length ());
Buf. append ("" + str. substring (0, 18). append ("foolishness ,");
System. out. println ("buf =" + buf );
System. out. println ("buf. length () =" + buf. length ());
System. out. println ("buf. capacity () =" + buf. capacity ());
System. out. println ("str =" + str );
}
}
Program output:
Buf = it was the age of wisdom.
Buf. length () = 25
Buf. capacity () = 41
Str = it was the age of wisdom
Str. length () = 25
Buf = it was the age of wisdom, it was the age of foolishness,
Buf. length () = 56
Buf. capacity () = 84
Str = it was the age of wisdom,
As you can see, after the content is changed, the capacity also changes.
Length increases with adding characters to a string
The capacity is increased only after the new length exceeds the current capacity.
The StringBuffer capacity is automatically changed when the operating system needs it.
What programmers can do for capacity is to forcibly allocate a fixed capacity to the StringBuffer object during initialization. However, it is willing to look at itself completely without changing it. capacity will change with the length exceeding. In short, it is always larger than length.
 
5. Don't jump over your rudder.
The appetite of StringBuffer looks good, and it seems that it can become an aircraft carrier at will.
Note:
For every change and change of capacity, the entire object must be rebuilt and moved to another memory zone. To avoid similar overflow, it is best to allocate sufficient capacity for it during initialization.
If you are a captain, you suddenly ordered: "We are about to abandon the ship, another large ship is going !", What do you think of sailors? "Let's stop ourselves ?", So try to prepare a proper ship before departure. Do not overload or be too large. Never overturn the ship in the middle!
StringBuffer also has a naughty method to show off: reverse ()
It can reverse the content in its stomach.
 
6. Ma eats pony
Oh, my God, no one can use String or StringBuffer.
What should we do?
(1) If your String appears only a few times, let them go. After all, the grassland is vast.
(2) If the number of them is astonishing and messy as a group of destruction everywhere, intern them () and learn to swim better than drowned
In addition, combined with the use of StringBuffer, it is similar to the headers of this group of Trojans, which can be used to teach the wild strings in a comfortable manner, and combine their headers, tails, and tails to form a group, it is easier to control and manage (but it is frustrating that its editing function is not very good)
But be careful. Although StringBuffer has a big belly, don't break it down. If you give it a space, it will play a better role.
(2) treat with caution = and equals ()
Never forget this. They are all objects.
You can try it in a variety of efficient ways (intern () and then =)
However, in principle, we must be sure that
(3) the String and StringBuffer cannot use equals () for each other ()
Don't forget that they are not similar

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.