Wednesday, 30 March 2016

Java Tutorial : Java String [intern() method]


Click here to watch in Youtube :
https://www.youtube.com/watch?v=vx2iB6ahAYE&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java String [intern() method] 
InternDemo.java
/*
 * public String intern()
 *  
 * Returns: 
 * -------
 * a string that has the same contents as 
 * this string, but is guaranteed to be 
 * from a pool of unique strings.
 */
public class InternDemo
{
    public static void main(String[] args)
    {
        String s1 = new String("hello");
        String s2 = "hello";

        /*
         * returns string from pool, now it will be same as
         * s2
         */

        String s3 = s1.intern();
        System.out.println(s3);

        /*
         * false because reference is different
         */
        System.out.println(s1 == s2);

        /*
         * true because reference is same
         */
        System.out.println(s2 == s3);

    }
}
Output
hello
false
true
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/StringDemo_intern_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StringDemo_intern_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/89dfdef018d2970576505c9f498b5e4f28628c35/BasicJava/StringDemo_intern_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment