To test the intern method of the String class

Source: Internet
Author: User

<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

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.