The difference between string new and direct assignment in Java

Source: Internet
Author: User

The difference between string new and direct assignment in Java

For strings: References to their objects are stored in the stack, and if the compilation period has been created (defined directly in double quotes), it is stored in a constant pool, which is stored in the heap if the run-time (new) is determined. For a string equal to equals, there is always only one copy in the constant pool, with multiple copies in the heap.

For example:

String str1= "abc"; and String str2 = new String ("abc");

String str1= "ABC" might create an object or not create an object, and if the string "ABC" does not exist in the Java string pool, it will create a string object ("abc") in the Java string pool. If it already exists, STR1 direct reference to the object in this string pool.

String str2 = new String ("ABC") You can create at least one object, or two. Because the new keyword is used, a str2 string object is created in the heap, and its value is "ABC". Also, if the string "ABC" does not exist in the Java string pool, it creates a string object in the Java string pool (" ABC ").

String has a intern () method, native, used to detect whether the string exists in the string pool.

Public String Intern ()

Returns a normalized representation of a string object.

An initially empty string pool, which is privately maintained by the class string.

When the Intern method is called, if the pool already contains a string equal to this string object (which is determined by the Equals (object) method), the string in the pool is returned. Otherwise, this string object is added to the pool, and a reference to this string object is returned.

It follows for any two strings s and T, and s.intern () = = T.intern () is true only if and only if S.equals (t) is true.

All literal strings and string-assignment constant expressions are internal.

Return:

A string with the same content as this string, but it is guaranteed to come from a string pool.

Consider the following question:

New String ("abc"New string ("abc");

STR1 = = is the value of str2 true or false? False.

String STR3 = "ABC";
String STR4 = "ABC";

String STR5 = "A" + "BC";

STR3 = = is the value of STR4 true or false? True.

STR3 = = is the value of STR5 true or false? True.

When writing code, generally do not string str2 = new String ("ABC");

String a = "ABC"; String b= "AB"; String c=b+ "C"; System.out.println (afalse


Both A and B are string constants, so they are determined at compile time!

A b in C is a reference not a string constant, so it is not determined at compile time.
And the string is final! So when the B + "C" is actually a new object created, then the reference to the newly created object is passed to C.

 Public Static voidMain (string[] args)throwsException {String a= "B" ; String b= "B" ; System.out.println (A==b); String D=NewString ("D"). Intern (); String C= "D" ; //string d = new String ("D"). Intern (); System.out.println (c = =d); System.out.println ("------------------"); String D1=NewString ("D" ) ; String E1=D1.intern (); String C1= "D" ; //string d = new String ("D"). Intern (); SYSTEM.OUT.PRINTLN (C1 = =D1); SYSTEM.OUT.PRINTLN (C1==E1); SYSTEM.OUT.PRINTLN (E1==D1); System.out.println ("------------------"); String S1=NewString ("Kvill"); String S2=S1.intern (); System.out.println (S1==S2);//S1=s1.intern ()System.out.println (s1+ "" +S2); SYSTEM.OUT.PRINTLN (S2==S1.intern ()); }    


Operation Result:

True
True
------------------
False
True
False
------------------
False
Kvill Kvill
True

S1==s1.intern () to False indicates that the original "Kvill" still exists;

Example code:

String S1 = "China";    = "China";   = "China";    New String ("China");    New String ("China");    New String ("China");     

Explain here that for a string to be generated by new (assuming "China"), the constant pool is first searched for whether the "China" object is already in place, and if none is created in the constant pool, then a copy object of this "China" object in the constant pool is then created in the heap.

That's the way to interview: string s = new string ("xyz"), how many objects are produced?

one or two of them. If there is no "xyz" in a constant pool, it is two. If there is "XYZ" in the original constant pool, it is one.

For variables and constants of the underlying type: variables and references are stored in the stack, and constants are stored in the constant pool.

The difference between new string and string pool in Java is a waste of space, why do you need it?

For the following program: SS0 = new String ("Hello"), the new object is created with new () and is stored in the heap. A new object is created each time the call is made. Of course, from a space-saving point of view, certainly not as str= "Hello", have children's shoes must ask, then what is the use of it? When designing the compiler, why design it? Mark-to-win, then I ask you, if you do not know the contents of the string when you compile the program? The new String is used, so there is nothing to use.

The difference between string new and direct assignment in Java

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.