memory allocation in java string
March 8, 2011
java string and string heap memory allocation pool 2008-10-04 21:57
excerpt:
Java Runtime Environment has a string pool, by the String class maintenance. Execute the statement String str = “abc”, the first check whether there is a string pool string “abc”, if there is direct to “abc” is assigned to str, if there is a new first string in the string pool “abc”, then it is assigned to str. Execute the statement String str = new String (“abc”), regardless of the existence of the pool string string “abc”, directly to a new string “abc” (note: the new string “abc” is not in the string pool in), and then paid str. Previous statements, high efficiency, low efficiency of the latter statement, because the new strings take up memory space. String str = new String () creates an empty string, and the String str = new String (“”) the same.
public String intern strong> ()
standardized returns a string representation of the object. An initially empty string pool, which consists of class String privately maintained.
When the intern method is invoked, if the pool already contains a string equal to this String object (with the equals (Object) method to determine), the string is returned to the pool. Otherwise, this String object is added to the pool, and returns a reference to this String object.
it follows these rules: For any two strings s and t, if and only if s.equals (t) is true, s.intern () == t.intern () it is true.
String.intern ();
add a little introduction: the existence of. class file constant pool jvm loaded during the run, and can be expanded. String intern () method is a method of expansion of the constant pool; when an instance of String str call intern () method, java find the constant pool have the same unicode string constants, and if so, then return to their reference, if not , the increase in the constant pool is equal to a unicode string str and returns a reference.
Example 3:
String s0 = “kvill”;
String s1 = new String (“kvill”);
String s2 = new String (“kvill”);
System.out.println (s0 == s1);
S1.intern ();
S2 = s2.intern ();
System.out.println (s0 == s1 );
System.out.prntln (s0 == s1.intern ());
System.out.println (s0 == s2);
results:
False
False / / despite the implementation of s1.intern (), but its return value is not assigned to s1
True
True
finally get rid of a misunderstanding:
Some people say, “Use String.intern () method of a String class can be saved to a global String table, if the unicode string with the same value already in the table, then the method returns a string in the table have been address, if the table does the same string value, then their address registered to the table “if the global String table to eat, then be understood as a constant, the last sentence” if not the same value in the table of characters string, it will register its own address to the table “is wrong.
Example 4:
String s1 = new String (“kvill”);
String s2 = s1.intern ();
System.out.println (s1 == s1. intern ());
System.out.println (s1 “” s2);
System.out.println (s2 == s1.intern ());
result: < br /> False
Kvill kvill
True
we do not declare a “kvill” constant, so the beginning of the constant pool is not “kvill”, when we call s1.intern () after the Add in the constant pool of a new “kvill” constant, the original is not in constant pool “kvill” still exists, it is not “put their address registered to the constant pool” of.
wuwu Notes strong>
Example 5:
String str1 = “java”; / / pointer to the string pool
String str2 = “blog”; / / pointer to the string pool
String s = str1 str2; / / s is pointing to the heap is “javablog” object in the heap operator to set up two String objects, the value of these two objects are the “java” “blog”. that is copied from the string pool, these two values, then create two objects in the heap, and then create the object s, and then “javablog” heap address is assigned to s. sentence were created? a String object!
System.out.println (s == “javablog “);// results are false.
Jvm does type such as String str1 = “java”; of the String object on the constant pool, but it is done at compile time, so, while String s = str1 str2; is to know at run time, That str1 str2 is created on the heap, so the result is false the.
If you change the look of two ways:
String s = “java” “blog”; / / directly to the “javablog” into the string pool, System.out.println (s == “javablog”); the result is true, this sentence created? a String object
String s = str1 “blog”; / / do not put the string pool, but in the heap allocation, System.out. println (s == “javablog”); the result is False, this sentence created? a String object
answer: strong>
String s = new String (“abc”) ; create a few String objects?
String s = new String (“abc”); create a few String objects?
the difference between an object reference variable;
string literal “abc” is a String object;
text pool (pool of literal strings) and heap (heap) in the string object.
First, the object reference variable: in addition to some of the early Java books and now the garbage books, people can learn more clearly from the difference between the two.
A aa;
This statement declares a reference variable of class A aa [we often call handle], while the object is generally created by the new. So the title is just a reference variable s, it is not the object.
Second, Java strings in all text [string constants] is a String object. Some people [especially C programmers] In some occasions, like the string “as / as” an array of characters, which no solution because there are some strings and character arrays are intrinsically linked. In fact, with an array of characters are two completely different objects.
System.out.println (“Hello”. length ());< br /> char [] cc = {;
System.out.println (cc.length);
three , the string object is created:
extensive use of the object as a string (it is an object, in general, objects are always allocated in the heap memory), Java in order to save memory space and running time (such as comparing strings when == than equals () fast), all at compile time to put text into a text string pool (pool of literal strings), while the runtime constant pool of the text as part of the pool. Text pool advantage is that the pool all the same string constants are combined, it only takes up a space.
We know, for two reference variables, use == to determine their value (reference) are equal, that point to the same object:
String s1 = “abc”;
String s2 = “abc”;
if (s1 == s2) System.out.println (“s1, s2 refer to the same object”);
else System.out.println (“trouble”); < br /> Here output shows that two strings saved as a text object. That is, only in the pool above code creates a String object.
now see String s = new String (“abc”); statement, where “abc” itself is an object of the pool, and in the run-time execution new String () when,
in the pool copy the object into the heap, and the heap in a reference to this object s holding. ok, this statement would create two String objects.
String s1 = new String (“abc”);
String s2 = new String (“abc”);
if (s1 == s2) {/ / do not execute the statement}
judge will know when to use ==, although the two objects of the “content” the same (equals () to determine), but held two references refer to different variables,
The above code creates a few a String Object? (three, pool in a, heap of 2.)
wuwu summary strong>
In summary, there are two ways to create strings: Two memory area (pool, heap)
1, “” quotes in the string pool created string
2, new, new string is created, you first see whether the pool has the same value of the string, If so, then a copy of the heap, and then return the address of the heap; if the pool does not, then create a heap, and then return the address of the heap (note that at this time do not need to copy to the pool from the heap , otherwise, the string will make the heap is always a subset of the pool, resulting in wasted space in the pool)!
In addition, the assignment of a string, if the right operand contains one or more reference string, then the heap and then create a string object, return a reference; such as String s = str1 “blog” ;
compare two strings already in the string pool object can use the “==” for, with more than equals operator faster strong>
