Transferred from: http://txy821.iteye.com/blog/760957
Java.lang.String's Intern () method
The return value of the "ABC". Intern () method is also the string "abc", which looks as if this method is of little use. But in fact, it made a little gesture:
Check if there is an "ABC" string in the string pool, return the string in the pool if it exists, and if it does not, the method adds "ABC" to the string pool and returns its reference.
Let's do a test:
Java code
- String str1 = "a";
- String str2 = "BC";
- String STR3 = "a" +"BC";
- String STR4 = STR1+STR2;
- System.out.println (STR3==STR4);
- STR4 = (STR1+STR2). Intern ();
- System.out.println (STR3==STR4);
The result of the output will be:
False
True
The JDK's API documentation explains this:
=======================================================================
Returns a normalized representation of a string object.
An initially empty string pool, which is privately maintained by the class string.
When the Intern method is called, if the pool already contains a string equal to this string object (which is determined by the Equals (object) method), the string in the pool is returned. Otherwise, this string object is added to the pool, and a reference to this string object is returned.
It follows for any two strings s and T, and s.intern () = = T.intern () is true only if and only if S.equals (t) is true.
All literal strings and string-assignment constant expressions are internal. String literals are defined in the §3.10.5 of Java Language specification.
Return:
A string with the same content as this string, but it is guaranteed to come from a string pool.
=======================================================================
A string literal pool refers to a constant pool.
There are two ways to create a string object
As follows:
string S1 = new String (""); First Kind
String s2 = ""; The second Kind
The first one never sinks into the pool.
The second depends on the situation (equal to the right if the constant is in the pool, the very amount is not into the pool)
Cases:
String s3 = "a" + "B"; "A" is a constant, "B" is a constant, a constant + constant = constant, so it sinks into the pool.
String S4 = S1 + "B"; S1 is a variable, "B" is a constant, variable + constant! = constant, so it does not enter the pool.
Once in the pool, you will find the object in the pool first. If this object is available, the object reference is directed to the object, and if there is none, the object is created first, and then the object reference points to the object.
Cases:
String S5 = "ABC"; Find the "ABC" object in the pool first, and if so, let S5 point to the object, or if there is no "ABC" object in the pool, create an "ABC" object in the pool and let S5 point to the object. Add:
After looking at the bytecode, I found
String str = "a" + "B";
is exactly the same as
String str= "AB";----------------------attach a small instance: Java code
- Public class Mud {
- Public static string Hello (string[] strs, string s2) {
- strs[0] = "<" + strs[0] + ">";
- S2.touppercase ();
- return S2;
- }
- /**
- * @param args
- */
- Public static void Main (String ... args) {
- String a = new string ("T");
- String[] B = new string[] { "T"};
- String C = A.intern ();
- if (a.equals (b[0])) {
- System.out.print ("1");
- }
- if (b[0] = = c) {
- System.out.print ("2");
- }
- if (a = = c) {
- System.out.print ("3");
- }
- A = Hello (b, c);
- System.out.print (a);
- System.out.print (b[0]);
- System.out.print (c);
- }
- }
Stack-to-heap differences: http://droidyue.com/blog/2014/12/07/differences-between-stack-and-heap-in-java/
String constants in Java, differences in stack and string Functions intern ()