BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String intern() method explained with examples

By Chaitanya Singh | Filed Under: java

Java String intern() method is used for getting the string from the memory if it is already present. This method ensures that all the same strings share the same memory. For example, creating a string “hello” 10 times using intern() method would ensure that there will be only one instance of “Hello” in the memory and all the 10 references point to the same instance.

A Simple Java String intern() method example

This example demonstrates the use of intern() method. This method searches the memory pool for the mentioned String, if the string is found then it returns the reference of it, else it allocates a new memory space for the string and assign a reference to it.

public class Example{  
   public static void main(String args[]){  
	String str1 = "beginnersbook";    
		
	/* The Java String intern() method searches the string "beginnersbook"  
	 * in the memory pool and returns the reference of it.
	 */
	String str2 = new String("beginnersbook").intern(); 
	//prints true 
	System.out.println("str1==str2: "+(str1==str2));
   }
}

Output:

str1==str2: true

Java automatically interns String literals

When we create Strings using string literals instead of creating them using new keyword then the java automatically interns String them. Lets take an example to understand this:

public class Example{  
   public static void main(String args[]){  
	String str1 = "Hello";
		
	//Java automatically interns this	
	String str2 = "Hello";
		
	//This is same as creating string using string literal		
	String str3 = "Hello".intern();
		
	//This will create a new instance of "Hello" in memory
	String str4 = new String("Hello");
		
	

	if ( str1 == str2 ){
	    System.out.println("String str1 and str2 are same");
	}

	if ( str2 == str3 ){
	    System.out.println("String str2 and str3 are same" );
	}

	if ( str1 == str4 ){
	    //This will not be printed as the condition is not true
	    System.out.println("String str1 and str4 are same" );  
	}

	if ( str3 == str4 ){
	    System.out.println("String str3 and str4 are same" );  
	}
   }
}

Output:

String str1 and str2 are same
String str2 and str3 are same
String str3 and str4 are same

Related Posts:

  1. Java String format() method
  2. Java String matches() method
  3. Java String trim() and hashCode() methods
  4. Java String replace(), replaceFirst() and replaceAll() methods

References:

  • intern() method – JavaDoc
  • What is String interning?
  • Purpose of intern() method in Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap