Home » Core Java » lang » String » Java String intern Example

About Chryssa Aliferi

Chryssa Aliferi
Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.

Java String intern Example

In Java, when we talk about String interning, we describe how Java stores only one copy of every distinct String value in the string pool, in order to reuse String objects to save memory from a program. This practically means, that each String object is stored only once in memory, regardless of how many times the same String may appear in the code.

So, as you already guessed, in this example we are going to talk about interning Strings and comparing them with the use of intern() method.

String intern() method:

The most common methods for String comparison are the equals() and equalsIgnoreCase() methods. However, these methods may need large amount of memory for large sequence of characters. The Java String intern() method helps us to improve the performance of the comparison between two Strings.

The intern() method, when applied to a String object, returns a reference to this object (from the hash set of Strings that Java makes), that has the same contents as the original object. Thus, if a code uses the intern() method for several String objects, then our program will use significantly less memory , because it will reuse the references of the objects in the comparison between these Strings.

Keep in mind, that Java automatically interns String literals. This means that the intern() method is to be used on Strings that are constructed with new String().

Example:

JavaStringIntern.java

package com.javacodegeeks.javabasics.string;

public class JavaStringIntern {

    public static void main(String[] args) {

        String str1 = "JavaCodeGeeks";
        String str2 = "JavaCodeGeeks";
        String str3 = "JavaCodeGeeks".intern();
        String str4 = new String("JavaCodeGeeks");
        String str5 = new String("JavaCodeGeeks").intern();

        System.out.println("Are str1 and str2 the same: " + (str1 == str2));
        System.out.println("Are str1 and str3 the same: " + (str1 == str3));
        System.out.println("Are str1 and str4 the same: " + (str1 == str4)); //this should be "false" because str4 is not interned        
        System.out.println("Are str1 and str4.intern() the same: " + (str1 == str4.intern())); //this should be "true" now                
        System.out.println("Are str1 and str5 the same: " + (str1 == str5));

    }
}

Output:

Are str1 and str2 the same: true
Are str1 and str3 the same: true
Are str1 and str4 the same: false
Are str1 and str4.intern() the same: true
Are str1 and str5 the same: true 


As a conclusion, the
intern() method, can be very useful when we want to search through Strings, or when we want to retrieve information from a large text. The best practice is to use String.intern() on Strings that occur multiple times inside a program, and do this only when you want to save memory. It will be effective depending on the ratio of unique versus duplicate String objects.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of