String constants in Java, differences in stack and string Functions intern ()

Source: Internet
Author: User

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
    1. String str1 = "a";
    2. String str2 = "BC";
    3. String STR3 = "a" +"BC";
    4. String STR4 = STR1+STR2;
    5. System.out.println (STR3==STR4);
    6. STR4 = (STR1+STR2). Intern ();
    7. 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
  1. Public class Mud {
  2. Public static string Hello (string[] strs, string s2) {
  3. strs[0] = "<" + strs[0] + ">";
  4. S2.touppercase ();
  5. return S2;
  6. }
  7. /**
  8. * @param args
  9. */
  10. Public static void Main (String ... args) {
  11. String a = new string ("T");
  12. String[] B = new string[] { "T"};
  13. String C = A.intern ();
  14. if (a.equals (b[0])) {
  15. System.out.print ("1");
  16. }
  17. if (b[0] = = c) {
  18. System.out.print ("2");
  19. }
  20. if (a = = c) {
  21. System.out.print ("3");
  22. }
  23. A = Hello (b, c);
  24. System.out.print (a);
  25. System.out.print (b[0]);
  26. System.out.print (c);
  27. }
  28. }

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 ()

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.