Java Intern () method

Source: Internet
Author: User

Intern () Method:

Public String Intern ()

JDK source code such as:

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 (the object is determined by the Equals (object) method), the

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 expressions are internal.

Return:

A string with the same content as this string, but it is guaranteed to come from a string pool.

Although calling the Intern method in the output has little effect, the background method actually does a series of actions and actions.

"AB" is returned when calling the "AB" Intern () method, but this method first checks to see if there is a string "AB" in the string pool.

Returns a reference to the string if it exists, otherwise the string is added to the string pool, and the reference to the string is returned.

For example:

public class Str2{public static void Test () {String a= "a"; String b=a+ "B"; String c= "AB"; String D=new string (b); System.out.println (B==C); System.out.println (D==C); System.out.println (C==d.intern ()); System.out.println (B.intern () ==d.intern ());} public static void Main (String []args) {new STR2 (). Test ();}}

Java Intern () method

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.