The usefulness of the Intern method of string

Source: Internet
Author: User
Tags stmt

For the first time today, looking at effective Java, a bit of the static factory approach in its first item says, "They're called every time.

, don't have to create a new object "and mention it at the end---" The String.intern method implements this in a limited form

Kind of optimization ", because previously did not use intern this method, so went to check, and write down their understanding for future reference
First look at the description of its Chinese API:


Intern

Public String Intern ()
Returns a normalized representation of a string object.
A string pool that is initially empty, maintained by a class string private. When the Intern method is invoked, the string in the pool is returned if the pool already contains a string that is equal to this string object, which is determined by the Equals (object) method. Otherwise, this string object is added to the pool and a reference to this string object is returned. It follows that s.intern () = = T.intern () is true for any two strings s and T, if and only if S.equals (t) is true


summed up its meaning as follows:
if: S.intern () method, the string in the shared pool is compared to the external string (s). If there is a string equal to it in the shared pool, the external string is not placed in the shared pool, only the string in the shared pool is returned, and the external string is placed in the shared pool if different. and returns the handle (reference) of its string-the advantage of doing so is to save space


Finally, let's look at a good example of this.

Before the example, let's assume that we have the test database in sqlserver2000, which has the following table:
Test1
Name
We add 8,000 records to the inside through the following procedure:

Import java.sql.*;

public class TestDB {

private static String drivername = "Com.microsoft.jdbc.sqlserver.SQLServerDriver";

private static String Dburl =

"Jdbc:microsoft:sqlserver://localhost:1433;databasename=test";

private static String UserName = "sa";

private static String userpwd = "";

private static Connection dbconn;

public static void Main (string[] args) {

try {
Class.forName (drivername);
Dbconn = Drivermanager.getconnection (Dburl, UserName, userpwd);
Statement stmt = Dbconn.createstatement ();
String sql = "INSERT into test1 values (' 123456789123456789123456789 ');";
for (int i = 0; i < 8000; i++) {
Stmt.executeupdate (SQL);
}
System.out.println ("Connection successful!");
catch (Exception e) {
E.printstacktrace ();
}
}
}

After the addition completes, we execute in the Query Analyzer:
Select COUNT (*) from test1;
You can see the result: 8000 indicates that 8,000 data is inserted correctly

Suppose we have such a class: Po.java
public class Po {
private String name;

public void SetName (String s) {
name = S;
}

}

We perform the following classes: Teststringintern.java

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.Statement;
Import java.util.ArrayList;
Import java.util.List;

public class Teststringintern {
private static String drivername = "Com.microsoft.jdbc.sqlserver.SQLServerDriver";

private static String Dburl =

"Jdbc:microsoft:sqlserver://localhost:1433;databasename=test";

private static String UserName = "sa";

private static String userpwd = "";

private static Connection dbconn;

private static list<po> list= new arraylist<po> ();
public static void Main (string[] args) {

try {
Class.forName (drivername);
Dbconn = Drivermanager.getconnection (Dburl, UserName, userpwd);
Statement stmt = Dbconn.createstatement ();
ResultSet rs = stmt.executequery ("Select name from Test1");
while (Rs.next ()) {
String s = rs.getstring (1);;
PO p = new PO ();
P.setname (s);
List.add (P);
s = null;
p = null;
}
Long total = Runtime.getruntime (). TotalMemory ();
Long free = Runtime.getruntime (). Freememory ();
System.out.println ("The Busy Memory is:" + (Total-free));
Rs.close ();
Stmt.close ();
Dbconn.close ();
System.GC ();
catch (Exception e) {
E.printstacktrace ();
}
}
}
We can see that the results of the execution are: the busy memory is:1252880
Note: If the sentence p.setname (s) is replaced by P.setname (S.intern ());
Then execute the program we can see the result: the busy memory is:515944
We can go through subtle changes we can see that the memory it occupies is not an order of magnitude
So what does this intern () function do? I think:
The string "123456789123456789123456789" stored in a private pool maintained by a string object may have only one, because each time is the same as the pair, so the object is not repeated in the pool, so call intern () The method is to complete this function, which returns only the same string inside, without creating a new string, so it won't take up too much space

Note: Many of the above are just personal understanding, the specific implementation details are not clear, so it will inevitably be wrong place, I hope to point out.

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.