×

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax:
public String intern()
Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1
public class JavaStringInternEx1
{ 
public static void main(String args[])
{ 
String s1=new String("Nitish");
System.out.println("String s1 : "+s1);
String s2="Nitish";
System.out.println("String s2 : "+s2);
String s3=s1.intern();//returns string from pool, now it will be same as s2 
System.out.println("Interned String is: " +s3);
System.out.println("s1 == s2---> "+(s1==s2));//false because reference variables are pointing to different instance 
System.out.println("s2 == s3---> "+(s2==s3));//true because reference variables are pointing to same instance 
}
  }
Output:
String s1 :
NitishString s2 :
NitishInterned String is: Nitishs
1 == s2---> falses
2 == s3---> true
Java String intern() Example 2
public class JavaStringInternEx2 {
    public static void main(String[] args) {
        String s1 = "TutorialAndExample.com";
        String s2 = "TutorialAndExample.com";
        String s3 = new String("TutorialAndExample.com");
        final String s4 = s3.intern();
        System.out.println("s1 == s2---> "+(s1 == s2));
        System.out.println("s2 == s3---> "+(s2 == s3));
        System.out.println("s3 == s4---> "+(s3 == s4));
        System.out.println("s1 == s3---> "+(s1 == s3));
        System.out.println("s1 == s4---> "+(s1 == s4));
        System.out.println("s1 equals s2---> "+(s1.equals(s2)));
        System.out.println("s2 equals s3---> "+(s2.equals(s3)));
        System.out.println("s3 equals s4---> "+(s3.equals(s4)));
        System.out.println("s1 equals s4---> "+(s1.equals(s4)));
        System.out.println("s1 equals s3---> "+(s1.equals(s3)));
    }
}
output:
s1 == s2---> trues
2 == s3---> falses
3 == s4---> falses
1 == s3---> falses
1 == s4---> trues
1 equals s2---> trues
2 equals s3---> trues
3 equals s4---> trues
1 equals s4---> trues
1 equals s3---> true

Related Topics

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

Java Externalization

What is Externalization in Java? Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine...

3 minutes read.

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Java Do While Loop

When we wish to test the exit condition at the end of the loop, we use a do-while loop. The do-while loop always executes its body at least once, because...

1 minute read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

How to get Day Name from Date in Java

We'll write a Java application to extract the day's name from the Date in this section. When dealing with Date and time in Java, the following classes are used. Class for Calendars:...

6 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

Difference between Constructor and Method in Java

What is Constructor? In Constructor, we will discuss constructors and also will discuss default constructors and finally, we will discuss overloading constructors. A constructor is a method that is used to...

13 minutes read.