Immutable and resident pool of String type in Java

Source: Internet
Author: User

Basic Concepts

Preliminary definitions of Mutable and Immutable Objects:

Variable class: When an instance reference of this class is obtained, the content of this instance can be changed.

Immutable class: once an instance of an immutable class is created, the value of its inner member variable cannot be modified. Here, the String class is a classic application of the immutable class.

 


Example 2

Package cn. xy. test;

Public class StringTest
{

/**
* The value of a is determined during compilation, so its value "xy" is put into the resident pool of String (resident pool in heap) and pointed by.
* The value of B is also determined during compilation. If the value of B is in the resident pool of String, check whether there is a value equal to "xy". If yes, B points to it.
* The addresses of the two objects are the same.
* @ Return true
*/
Public static Boolean testString1 ()
{
String a = "xy ";
String B = "xy ";
Return a = B;
}
 
/**
* The value of B is the sum of two constants, which is also determined during compilation.
* @ Return true
*/
Public static Boolean testString2 ()
{
String a = "xyy ";
String B = "xy" + "y ";
Return a = B;
}

/**
* The value of B is the sum of a variable and a constant. It cannot be determined during compilation. Instead, it will generate an object with the value "abc" in the heap.
* @ Return false
*/
Public static Boolean testString3 ()
{
String a = "xyy ";
String B = "xy ";
B = B + "y ";
Return a = B;
}
 

/**
* The values of B cannot be determined during compilation, but a new object named "xyy" is generated in the heap ".
* @ Return false
*/
Public static Boolean testString4 ()
{
String a = "xyy ";
String B = "xy". concat ("y ");
Return a = B;
}

 
/**
* Strings created by new String () are not constants and cannot be determined during compilation. Therefore, strings created by new String () are not placed in the constant pool and have their own address space.
* The values of a and B cannot be determined during compilation. a new object with the value "xy" is generated in the heap.
* @ Return fasle
*/
Public static Boolean testString5 ()
{
String a = new String ("xy ");
String B = new String ("xy ");
Return a = B;
}

/**
* Intern () assigns the reference of "xy" in the resident pool to B.
* @ Return true
*/
Public static Boolean testString6 ()
{
String a = "xy ";
String B = new String ("xy ");
B = B. intern ();
Return a = B. intern ();
}

 

/**
* The toString method of char returns the string of a char object, instead of the "xy"
* @ Return false
*/
Public static Boolean testString7 ()
{
String B = "xy ";
Char [] a = new char [] {'x', 'y '};
Return a. toString (). equals (B );
}
 


/**
* Char is a new type and does not have the concept of resident pool.
* @ Return fasle
*/
Public static Boolean testString8 ()
{
String B = "xy ";
Char [] a = new char [] {'x', 'y '};
Return a. toString () = B;
}

 

/**
* String immutable embodiment
*/
String str = "xy ";

Public String chage (String str)
{
Str = "xyy ";
Return str;
}

 

/**
* Generally, the variability of the reference type (the address is passed when the value is passed, which is equivalent to handing over the warehouse to the method and getting the key to move the things in the warehouse)
*/
Person p = new Person ("xy ");

Public String changePerson (Person p)
{
P. setName ("xyy ");
Return p. toString ();
}

Public static void main (String [] args)
{
Print (testString1 (); // true
Print (testString2 (); // true
Print (testString3 (); // fasle
Print (testString4 (); // false
Print (testString5 (); // false
Print (testString6 (); // true

Print (testString7 (); // false

Print (testString8 (); // false

StringTest t = new StringTest ();
Print (t. str); // xy
Print (t. chage (t. str); // xxy
Print (t. str); // xy

Print (t. p. toString (); // xy
Print (t. changePerson (t. p); // xyy
Print (t. p. toString (); // xyy
}

 
Public static void print (Object o)
{
System. out. println (o );

 

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.