Java string allocates memory space memo

Source: Internet
Author: User

Stack memory

Heap Memory

)

access speed

slower than stack memory

The allocated memory is managed by the Java Virtual Machine automatic garbage collector 。 Dynamically allocating memory size

Shared features

if not, open up a new space deposit value

Each new object is generated in heap memory at a time.

Value can be changed after creation

String cannot be changed after the class declaration

One, stack memory

base Type int, short, long, byte, float, double, boolean, Char and Object references

Sharing features of Stacks

String str1 = "abc";
String str2 = "abc";
System.out.println (STR1==STR2); True

1 , the compiler processes string str1 = "abc"; it creates a reference in the stack with a variable of str1, and then looks for the value of ABC in the stack, if it is not found, stores the ABC in, and then points the STR1 to ABC.

2 , and then processing string str2 = "abc", after creating the reference variable of B, because the value of ABC is already in the stack, it points str2 directly to ABC. In this way, the str1 and str2 both point to the ABC situation.

Second, heap memory

New , NewArray, Anewarray and Multianewarray, and other directives established

Note: When defining a class using a format such as String str = "ABC", we always want to assume, of course, that the object of the string class was created by Str. Worry about traps! The object may not have been created! Instead, it might just point to an object that was previously created. Only through the new () method can you guarantee that a new object is created each time. Because of the immutable nature of the string class, you should consider using the StringBuffer class to improve program efficiency when a string variable needs to change its value frequently.

Third, = = memory address comparison

String str1 = "abc";
String str2 = "abc";
System.out.println (STR1==STR2); True str1 and str2 simultaneously point to the same memory space in the stack memory

String STR3 = "abc";
String STR4 = new String ("abc");

System.out.println (Str3 = = STR4); Flase STR3 value in stack memory, STR4 value in heap memory

String Hello = "Hello";

String hel = "hel";

String lo = "Lo";

System.out.println (Hello = = "hel" + "lo"); True

// two constants added, first detect if there is a hello in the stack memory if yes, point to the existing stack in the Hello space

System.out.println (Hello = = "hel" + lo); Flase

System.out.println (Hello = = hel + lo); Flase

//lo is in the constant pool, does not check the stack memory, generates a new hello in the heap

Four, the equals value of the comparison

public boolean equals(Object anobject)

Compares this string to the specified object. The result is true if and only if the parameter is not NULL and is a String object that represents the same sequence of characters as this object.

String STR5 = "abc";
String STR6 = new String ("abc");

System.out.println (Str5.equals (STR6)); True STR5 value of STR6 is compared to the value of

v. Memory address of the value in the intern stack

Public String Intern ()

When you call the Intern method

1 , if the pool already contains a string equal to this string object (as determined by the Equals (Object) method), the string in the pool is returned.

2 , adds this string object to the pool, and returns a reference to this string object.

String s7 = new String ("abc");

String s8 = "abc";

System.out.println (s7 = = S7.intern ());//flase ;

System.out.println (S8 = = S7.intern ());//true

1. Check the stack memory for any ABC objects if there is

2. Point the S7 to the pool ABC

Java string allocates memory space memo

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.