<pre name= "code" class= "java" >package com.ckw.mianshi;/** * Test The Intern method of the String class * @author Administrator *intern () : Returns a string with the same contents as this string, but it is guaranteed to come from a string pool. */public class Stringofintern {public static void main (string[] args) {/** * Example 1 */string s0= "my"; String S1=new string ("my"); String S2=new string ("my"); System.out.println (S0==S1); S1.intern (); S2=s2.intern (); System.out.println (S0==S1);//Although S1.intern () is executed, its return value is not assigned to S1SYSTEM.OUT.PRINTLN (S0==s1.intern ()); System.out.println (S0==S2); System.out.println ("");/** * Example 2 */string s3=new String ("You"); System.out.println (S3==s3.intern ());//Where S3.intern () from the you,s3 of the constant pool you just created comes from you in the heap, not the same as String S4=s3.intern (); System.out.println (s3+ "" +s4); System.out.println (S4==s3.intern ());}}
Test results:
Falsefalsetruetruefalseyou Youtrue
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
To test the intern method of the String class