Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java String intern()

Learn to intern a string, and how the string literals differ from string objects. Java String.intern() is a native method and performs fast.

Lokesh Gupta

January 10, 2023

Java String class
Java String, String Constant Pool
Java String

The String.intern() in Java returns the reference of an equal string literal present in the string pool. If there is an existing string literal present in the string pool then its reference is returned. Otherwise, a new string with the same content is created, and the reference of the new string is returned.

Note that, internally, the string equality is checked with String.equals() method.

1. What is String Pool?

The String pool is a reserved memory area in heap memory that Java uses for storing string constants. Note that Java strings are immutable by default.

Java stores one and only one copy of every distinct string value in the string pool. It helps to reuse strings to save memory during program execution. There may be many references to a string in the running program, but there will be the only one copy of a string inside the string pool.

2. String Literals and Objects

In Java, we can create strings in two ways. The first way is to create string literal, and second way is to create an String object using the new keyword.

String str1 = "hello world";  //String literal

String str2 = new String("hello world");  //String object
  • When we create a String using the literal (it is also recommended), string literals are always created directly into the string pool.
  • When we create a string with new keyword, the string is created in the normal heap memory.

3. String.intern() Method

The String.intern() is a native method that returns a reference to an equal string literal present in the string pool. Note that all string literals are automatically created in the String pool, so the intern() method is useful to String objects created with the new keyword.

The the following program, name variable contains the reference to the String object in the heap. When we call intern(), a new string literal with content “Alex” is created in the string pool. The variable str contains the reference to the string literal created in the pool.

String strObject = new String("Alex");
String strLiteral = strObject.intern();

Assertions.assertFalse(strObject == strLiteral);   //Points to different objects

Assertions.assertTrue(strObject.equals(strLiteral));  //Same content

Any new string literal created with the same content will point to the object in the string pool.

String newLiteral = "Alex";

Assertions.assertTrue(strLiteral == newLiteral);

As a rule, any two strings s1 and s2, s1.intern() == s2.intern() is true if and only if s1.equals(s2) is true.

4. Conclusion

In this Java tutorial, we learned to intern a string, and how the string literals are different from string objects. The String.intern() is a natively implemented method and provides very high performance.

Happy Learning !!

References: String Java Doc

Comments

Subscribe
Notify of
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

String Examples

  • String Constant Pool
  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • Align Left, Right, Center
  • Immutable Strings
  • StringJoiner
  • Split a string
  • Escape HTML
  • Unescape HTML
  • Convert to title case
  • Find duplicate words
  • Left pad a string
  • Right pad a string
  • Reverse recursively
  • Leading whitespaces
  • Remove whitespaces
  • Reverse words
  • Find duplicate characters
  • Get first 4 characters
  • Get last 4 characters
  • (123) 456-6789 Pattern
  • Interview Questions

String Methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

Table of Contents

  • 1. What is String Pool?
  • 2. String Literals and Objects
  • 3. String.intern() Method
  • 4. Conclusion
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java String contains()

Next

Initialize an ArrayList in Java

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap