JAVA String type and prototype mode

Source: Internet
Author: User

As described in the example above, the variable A, a, and their value 10, 20 are present in the stack, so the reference to the string type is also present in the stack. While string ABC is present in a string constant pool, the new string object is present in the heap.

str="abc";System.out.print(str==str1);//true

When this line of code is executed, the JVM looks in the string pool to see if an object with the value "ABC" already exists, and if it does, no longer creates a new object, returns a reference to the existing object, or, if it does not, creates the object first, joins it to the string pool, and returns its reference. So this code creates an object .

String str1="abc";  System.out.print(str==str2);//true  

This is clear from the above explanation, when executing the second line of code, the "ABC" string object already exists in the constant pool, so it returns directly to the string object that already exists in the pool.

String str2="a"+"b"+"c";

Since constant strings are also determined at compile time, because "a", "B" and "C" are constants, the value of variable str2 can be determined at compile time. This line of code is compiled with string str= "abc"; it's the same, so this code just creates an object , but does it seem like we don't usually? generally using "+" to concatenate two strings will result in another new character object . Let's take a look at the following line of code to understand:

String str3="c";String str4="ab"+str3;System.out.print(str==str4);//false String str5="ab";System.out.print(str==str6);//false

From the above example, we can conclude that the two strings connected using "+" are themselves literal constant strings, and if there is such a concatenated string in the pool, the object will not be recreated, but the string object in the pool is directly referenced; As long as there is a string in the concatenated two strings that is not a literal constant (that is, it has already been defined), a new string object is generated (throw away special as the final definition string does not mention).
Let's take a look at what's different when new creates a string:

String str7=new String("abc");  

First, how many string objects does this line of code create? The answer is 2 . Since the new String ("ABC") is equivalent to "ABC", one is created as the original instance object placed in the heap, and the other is the "AAA" object placed in the constant pool, of course the STR7 itself is simply a reference, placed in the stack, to point to the object created in the heap.

string Str7=new string ("abc"); string Str8=new string ("abc"); System.out. Print (STR7.  Equals (STR8)); //truesystem.print (STR7==str8);  Falsesystem.print (str==STR7);  False                   

Since STR7 and Str8 are references to existing stacks, they create two object "ABC" respectively, although they have the same content (Str7.equals (str8) ==true), but the actual physical address is different, so the STR7==STR8 value is false, similarly, STR points to "ABC" in the constant pool, so STR==STR7 is also false.

About constant pool is to avoid the frequent creation and destruction of objects and affect system performance, which realizes the sharing of objects.
For example, a string constant pool in which all string literals are placed in a constant pool during the compilation phase.
(1) Save memory Space: All the same string constants in a constant pool are merged, occupying only one space.
(2) Save run Time: when comparing strings, = = is faster than Equals (). For two reference variables, only = = To determine whether the reference is equal, you can also determine whether the actual value is equal.

Note: Constant pooling is primarily used to hold two major classes of constants: Literal (Literal) and symbolic reference (symbolic References), which is equivalent to the concept of Java language-level constants, such as literal strings, constant values declared final, etc. Symbolic references are concepts of compilation principles.
So of course the constant pool does not just represent a string constant pool, for example some wrapper classes implement the constant pool technique .

Wrapper classes for basic types and basic types. The basic types are: Byte, short, char, int, long, Boolean. The basic types of wrapper classes are: Byte, short, Character, Integer, Long, Boolean (note case sensitive).
The difference between the two is that the basic type is the normal variable in the program, and the basic type of wrapper class is the class, which is the reference variable in the program. therefore, they are stored in memory in different locations: The base type is stored in the stack, and the base type wrapper class is stored in the heap.
These packaging classes mentioned above all implement the constant pool technology, and the other two types of floating-point type of packaging class is not implemented .
As follows:

 integer I1=10;        integer i2=10; integer i3=new Integer (10); integer i4=new Integer (10); System. out.print (I1==I2);//true System. out.print (I2==I3);//false System. out.print (I3==I4);//false Double d1=1.0; Double d2=1.0; System. out.print (D1==D2);//false          

Case Analysis:
1) I1 and I2 are reference types that store pointers in the stack because integer is the wrapper class. Because the integer wrapper class implements the constant pool technique, 10 of both I1 and I2 are obtained from the constant pool, all pointing to the same address, so i1=12.
2) i3 and I4 are reference types that store pointers in the stack because integer is the wrapper class. But since they are all new, they are no longer looking for data from the constant pool, they are new to each object from the heap, and then each hold a pointer to the object, so i3 and I4 are not equal because they have different pointers and different pointing objects.
3) D1 and D2 are reference types that store pointers in the stack because double is a wrapper class. But the double wrapper class does not implement a constant pool technique, so doubled1=1.0 is equivalent to double d1=new double (1.0), which is a new object from the heap, D2 the same. So D1 and D2 hold different pointers, they point to different objects, so they are not equal.

JAVA String type and prototype mode

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.