Non-denatured and resident pools of string types in Java

Source: Internet
Author: User

A basic concept

A preliminary definition of a mutable class and an immutable class (Mutable and immutable Objects):

Mutable class: When you get an instance reference to this class, you can change the contents of the instance.

Immutable class: An instance of an immutable class once created, the value of its intrinsic member variable cannot be modified. Where the string class is the classic application of the immutable class.


Two examples

Package cn.xy.test;

public class Stringtest
{

/**
* The value of a is determined at compile time, so its value "XY" is placed in the pool of strings (the pool resides in the heap) and is pointed to by a.
* The value of B is also determined at compile time, so the value of B is found in the pool of string to see if there is a value equal to "XY", and if so, the B point.
* Therefore two object addresses are consistent
* @return True
*/
public static Boolean TestString1 ()
{
String a = "XY";
String B = "XY";
return a = = B;
}

/**
* The value of B is added to two constants and is also determined at compile time.
* @return True
*/
public static Boolean TestString2 ()
{
String a = "XYY";
String B = "xy" + "Y";
return a = = B;
}

/**
* The value of B is the addition of a variable and a constant, which cannot be determined at compile time, but will be reborn as an object of value "ABC" in the heap.
* @return False
*/
public static Boolean TestString3 ()
{
String a = "XYY";
String B = "XY";
B = b + "Y";
return a = = B;
}

/**
* B's value can not be determined when compiling, but will be in the heap to be reborn into a separate object called "XYY".
* @return False
*/
public static Boolean TestString4 ()
{
String a = "XYY";
String B = "xy". Concat ("Y");
return a = = B;
}


/**
* The string created by new string () is not a constant and cannot be determined at compile time, so the strings created by new string () are not placed in the constant pool, they have their own address space.
* The value of A and B cannot be compiled when it is determined and will be reborn in the heap as an object with the value "XY".
* @return Fasle
*/
public static Boolean TestString5 ()
{
String a = new string ("XY");
String b = new string ("XY");
return a = = B;
}

/**
* Intern () assigns a reference to "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 ();
}

/**
* Char's ToString method returns the string of a Char object instead of the "XY" we imagined
* @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 there is no concept of resident pool.
* @return Fasle
*/
public static Boolean TestString8 ()
{
String B = "XY";
Char[] A = new char[]{' x ', ' Y '};
return a.tostring () = = B;
}

/**
* The embodiment of the non-variability of string
*/
String str = "XY";

public string chage (String str)
{
str = "XYY";
return str;
}

/**
* General reference type of variability (when the value of the time to pass the address, the equivalent of the warehouse to the method, the method to get the key to move the warehouse things)
*/
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);
}

}

Non-denatured and resident pools of string types 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.