String and String

Source: Internet
Author: User

String and String

Java String:

  • Create
  • Splicing

String is one of the common classes in java and is essentially a character array char []. The String class is a final class and cannot be inherited. For String creation, you can use new to create objects or assign values directly. However, the implementation mechanisms of these two creation methods are different. When it comes to object creation, we will think of the heap and stack. There is also a string pool concept here. JVM maintains a String pool, and the string objects in the pool cannot be repeated. The string pool does not belong to the stack, it is a constant pool.

I. Create

You can create a String object in either of the following ways:

String str1 = new String ("abc"); // create an abc object in the string pool and create an abc object in the heap. str1 in the stack points to this object String str2 = "abc "; // search for the abc object in the string pool. The abc object already exists. Direct str2 in the stack to this object.
Read the two lines of comments carefully. After the first line of code is executed, the memory

After executing the second line of code, the memory

It can be verified by memory comparison:

Think about the results first and continue!

String str1 = "abc";String str2 = "abc";String str3 = new String("abc");System.out.println(str1 == str2); System.out.println(str1 ==str3); System.out.println(str1 == "abc"); System.out.println(str3 =="abc"); System.out.println(str1 == str3.intern());System.out.println(str3 == str3.intern());
Running result:


Resolution:

= Compare the memory address,

Str1 = str2; // all point to the object in the string pool, so true is returned.

Str1 = str3; // str1 points to the object in the string pool and str3 points to the object in the heap. Therefore, false is returned.

Str1 = "abc"; // all point to the object in the string pool, so true is returned.

Str3 = "abc"; // str3 points to the heap memory object, and the other is the object in the string pool. Therefore, false is returned.

Str1 = str3.intern (); // all point to the object in the string pool and return true

Str3 = str3.intern (); // str3 points to the heap memory object and str3.intern () points to the object in the string pool. Therefore, false is returned.

The intern () method returns the canonicalized representation of the string object. That is to say, when intern () is called, if the string pool already contains a string equal to (equals) This string object, returns the string in the pool.

Ii. Splicing

Let's first think about the execution result:

<span style="white-space:pre"></span>String hello = "hello";String hel = "hel";String lo = "lo";System.out.println(hello == "hel"+"lo");System.out.println(hello == "hel"+"looo");System.out.println(hello== "hel" + lo);
Running result:

Resolution:

  • If + is connected to a constant, the system first checks whether there is hello in the string pool. If yes, it returns its address directly and does not create a new one.
  • If the + connection has an object type, a new object is directly generated in the heap.

Continue Thinking about String concatenation:

Because the String class is final, that is, once an object is created, its internal state cannot be changed. However, the concatenation operation changes the internal state of the String. In this conflict, to maintain the non-variability of the string, you have to create a new String object after splicing. That is to say, every time you perform a concatenation operation, a new object is generated. When a large number of pointing operations are performed, a large number of objects are created, which leads to performance problems.

To solve this problem, jkd provides a variable supporting class StringBuffer for the string class. As StringBuffer is variable, splicing only changes the internal data structure without creating new objects. Therefore, the performance is greatly improved.


How can I implement a class definition that contains the string type?

String is a reference type, but it also has some value type features.

String or reference
C #'s String declaration is a class String, of course, a reference is passed.
However, most of these questions are due to this situation:
String a = "aaa ";
String B =;
B = "bbb ";
Or several lines of code:
Public void Swap (string s1, string s2)
{
String temp = s1;
S1 = s2;
S2 = temp;
}
At this time, when the result is printed, it is found that the value of a has not changed, and Swap has not succeeded. At this time, there will be an illusion: Is there no reference?
Haha, the string won't be so rudely disrupt the rule of "declaring as class is to pass reference.
Analysis:
String a = "aaa"; // ==> a -----> new String ("aaa ")
String B = a; // ==> B -----> a, upload reference
B = "bbb"; // ==> B -----> new String ("bbb"), pass the reference, B points to a new String, and a has not changed.

The same is true for Swap functions. For example, if a and B are passed in (a = "aaa", B = "bbb "),
// S1 -----> a, s2 -----> B
String temp = s1; // temp -----> s1 ----->
S1 = s2; // s1 -----> s2 -----> B;
S2 = temp; // s2 -----> temp ----->
The result is that s1 and s2 are indeed Swap, but this result does not affect a and B.

Type of string in C #

String is a reference type, but it also has some value type features.

String or reference
C #'s String declaration is a class String, of course, a reference is passed.
However, most of these questions are due to this situation:
String a = "aaa ";
String B =;
B = "bbb ";
Or several lines of code:
Public void Swap (string s1, string s2)
{
String temp = s1;
S1 = s2;
S2 = temp;
}
At this time, when the result is printed, it is found that the value of a has not changed, and Swap has not succeeded. At this time, there will be an illusion: Is there no reference?
Haha, the string won't be so rudely disrupt the rule of "declaring as class is to pass reference.
Analysis:
String a = "aaa"; // ==> a -----> new String ("aaa ")
String B = a; // ==> B -----> a, upload reference
B = "bbb"; // ==> B -----> new String ("bbb"), pass the reference, B points to a new String, and a has not changed.

The same is true for Swap functions. For example, if a and B are passed in (a = "aaa", B = "bbb "),
// S1 -----> a, s2 -----> B
String temp = s1; // temp -----> s1 ----->
S1 = s2; // s1 -----> s2 -----> B;
S2 = temp; // s2 -----> temp ----->
The result is that s1 and s2 are indeed Swap, but this result does not affect a and B.

Related Article

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.