String s= "abc" and string S=new string ("abc") in Java

Source: Internet
Author: User

1. Stacks and heaps (heap) are places that Java uses to store data in RAM. Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set up stacks or heaps.

2. The advantage of the stack is that the access speed is faster than the heap, second only to the registers directly in the CPU. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. In addition, the stack data can be shared, see 3rd. The advantage of the heap is that the memory size can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand that the Java garbage collector automatically collects the data that is no longer in use. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

1 = =is to determine whether two objects are the same object2Equals is the judgment of the value3 String A =New String ("AAA");  4 string b = new string ("a" );  5 B + = "AA"  6 a==b // error  7 a.equals (b) // correct  8 Span style= "color: #008080;" > 9 10 11 12 13 14 except string and wrapper, Equals () and "==" no difference 15 but the string and the wrapper override Equals (), so in this case, equals () refers to the equality of the original values for the comparison string or encapsulated object, and "= =" is a comparison of two objects for the same object      

First, let's take a look at the semantics of variables in Java:

There are two kinds of semantics for Java variables, and the original type of variables is value semantics (value), that is, you assign a value to a primitive type variable, it changes the data value itself. A variable of an object type is a referential semantics, meaning that assigning a variable to an object type simply points it to another object, but does not change the value of the object that was originally referenced.

Then, let's look at the characteristics of string and how Java is handled specifically for Sting:

The characteristics of the string

1. The string class is final and cannot be inherited.
2, the String class is the essence of the character array char[], and its value cannot be changed.
3, the String class object has a special way to create, is directly specified such as String x = "abc", "ABC" represents a String object. X is the address of the "ABC" object, also called a reference to the "ABC" object.
4. String objects can be concatenated by "+". A new string is generated after concatenation.
5. The Java runtime maintains a string pool, Javadoc translation is very vague "string buffer". The string pool is used to hold the various strings produced in the runtime, and the contents of the strings in the pool are not duplicated. The normal object does not exist in this buffer pool, and the object created only exists in the stack area of the method.

6, a lot of ways to create strings, summed up there are three categories:
First, create a string using the new keyword, such as string S1 = new String ("abc");
Second, direct designation. For example, string s2 = "abc";
Third, use concatenation to generate a new string. For example, string s3 = "AB" + "C";

The creation of the string object
The creation of a string object also has many doorways, the key is to understand its principle.


Principle 1: When using any way to create a string object S=x, the Java runtime (the running JVM) takes this x to find the string object with the same contents in the string pool, and if it does not, creates a string s in the pool, otherwise it is not added to the pool.

In principle 2:java, whenever you use the New keyword to create an object, you must create a new object (in the heap or stack area).

Principle 3: Creating a String object using direct designations or concatenation with a pure string only checks the string in the maintenance string pool without creating one in the pool. However, the string object will never be created in the stack area again.

Principle 4: Creating a String object with an expression containing a variable will not only examine the maintenance of the string pool, but also create a string object in the stack area.

"Immutable Class"

Java for efficiency, special handling of string type--provides a string pool for string type
There are two ways to define a variable of type string:


String Name= "Tom";(string Name= "T" + "O" + "M" have the same effect as here)
String name =new string ("Tom")


If you use the first way, when you declare a string that is "Tom", it will use the original memory in the pool without reallocating the memory, that is, string saname= "Tom" will point to the same piece of memory. And if, in the second way, there is no "Tom" in the pool, it will reallocate a chunk of memory in the heap and define a new object.

In addition, the question about the type of string is immutable: The string type is immutable, that is, when you want to change a string object, such as Name= "Madding", the virtual machine does not change the original object, but instead generates a new string object. Then let name point to it, and if the original "Tom" does not have any objects to reference it, the virtual machine's garbage collection mechanism will receive it.

Finally, on the stack problem of defining a string

string s =new string () parses the heap with the stack, first defining S, or new string ()
1. String str1 = "abc";
System.out.println (str1 = = "abc");

Steps:
1) The stack opens up a space to store reference str1;
2) A space in the string pool that holds the string constant "ABC";
3) Reference str1 to the string constant "ABC" in the pool;
4) The address of the str1 refers to the constant "ABC" address, the output is true;

2. String str2 = new String ("abc");
System.out.println (str2 = = "abc");

Steps:
1) The stack opens up a space to store reference str2;
2) Create a space in the heap to store a new string object "ABC";
3) Reference str2 to the new string object "ABC" in the heap;
4) The object address of the str2 refers to the address in the heap, while the constant "ABC" address is in the pool and the output is false;

3. String str3 = new String ("abc");
System.out.println (STR3 = = str2);

Steps:
1) The stack opens up a space to store reference STR3;
2) Create a new space in the heap to store another (different from str2) new string object;
3) Reference STR3 point to the other newly created string object;
4) STR3 and str2 point to different string objects in the heap, the address is not the same, the output is false;

4. String STR4 = "a" + "B";
System.out.println (STR4 = = "AB");

Steps:
1) The stack opens up a space to store reference STR4;
2) According to the compiler to consolidate the known amount of optimization function, the pool to open a space to store the combined string constant "AB";
3) Reference STR4 to the constant "AB" in the pool;
4) STR4 refers to the constant "AB" in the pool, the output is true;

5. Final String s = "a"; Note: Here S is the final modifier, which is equivalent to a constant
String STR5 = s + "B";
System.out.println (STR5 = = "AB");

Steps:
With four

6. String S1 = "a";
String s2 = "B";
String STR6 = s1 + s2;
System.out.println (STR6 = = "AB");

Steps:
1) stack in the middle of the storage reference s1,s1 point to the pool string constant "a",
2) stack in the middle of the storage reference S2,s2 point to the pool string constant "B",
3) in the stack to open a middle storage reference STR5,
4) S1 + S2 restores a new string object "AB" by the last step of the StringBuilder by the ToString () method, so that a space in the heap holds this object,
5) Reference STR6 to the new string object restored in the heap (S1 + s2),
6) STR6 points to the object in the heap, while the constant "AB" is in the pool and the output is false

7. String STR7 = "abc". SUBSTRING (0, 2);
Steps:
1) Open a space in the stack to store reference STR7,
2) The substring () method restores a new string object "AB" (as distinct from STR6), which creates a space in the heap to hold the object,
3) Reference STR7 points to the new string object in the heap,

8. String str8 = "abc". toUpperCase ();
Steps:
1) Open a space in the stack to store reference STR6,
2) The toUpperCase () method restores a new string object "ABC", which does not open a new space to hold the string constant "abc",
3) Reference str8 to a new string object in the heap

9.String s= "ABC";

String s1=s;

SYSTEM.OUT.PRINTLN (s1== "abc");

s=s+ "Hello";

SYSTEM.OUT.PRINTLN (s1== "abc");

SYSTEM.OUT.PRINTLN (s== "abc");

Steps:

1) Open a space in the stack to store s;

2) Sting pool to open a space for the storage of "ABC", the stack to open a space to store variable S1;

3) System output True, a space in the heap to store "Abchello";

4) Reference S to "Abchello" in the heap;

5) The system outputs true, then outputs false;

String s= "abc" and string S=new string ("abc") in Java

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.