Java String intern() Method

The String.intern() method in Java is used to return a canonical representation of the string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. intern Method Syntax
  3. Examples
    • Interning a String
    • Comparing Interned Strings
    • Benefits of String Interning
  4. Conclusion

Introduction

The String.intern() method is a member of the String class in Java. It allows you to ensure that all identical strings are represented by the same String object, which can help save memory and improve performance in some cases.

intern Method Syntax

The syntax for the intern method is as follows:

public native String intern();

Examples

Interning a String

The intern method can be used to return a canonical representation of a string.

Example

public class InternExample {
    public static void main(String[] args) {
        String str1 = new String("Hello");
        String str2 = new String("Hello");

        String internedStr1 = str1.intern();
        String internedStr2 = str2.intern();

        System.out.println("str1 == str2: " + (str1 == str2)); // false
        System.out.println("internedStr1 == internedStr2: " + (internedStr1 == internedStr2)); // true
    }
}

Output:

str1 == str2: false
internedStr1 == internedStr2: true

Comparing Interned Strings

Interning strings ensures that identical strings share the same memory reference, making it easy to compare strings using the == operator.

Example

public class InternExample {
    public static void main(String[] args) {
        String str1 = "Java";
        String str2 = new String("Java");

        String internedStr2 = str2.intern();

        System.out.println("str1 == str2: " + (str1 == str2)); // false
        System.out.println("str1 == internedStr2: " + (str1 == internedStr2)); // true
    }
}

Output:

str1 == str2: false
str1 == internedStr2: true

Benefits of String Interning

String interning can be beneficial in situations where you have many identical strings, as it reduces memory usage by ensuring that only one instance of each unique string is stored.

Example

public class InternExample {
    public static void main(String[] args) {
        String[] strings = new String[1000];

        for (int i = 0; i < strings.length; i++) {
            strings[i] = ("String" + i % 10).intern();
        }

        System.out.println("Memory saved by interning strings.");
    }
}

Output:

Memory saved by interning strings.

Conclusion

The String.intern() method in Java is used for optimizing memory usage and performance by ensuring that identical strings are represented by the same String object. By understanding how to use this method, you can efficiently manage strings in your Java applications. Whether you are interning strings, comparing interned strings, or taking advantage of the benefits of string interning, the intern method provides a reliable solution for these tasks.

Comments