×

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

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Java Program to find the smallest element in a tree

The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the...

3 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Java program to find frequency of characters in a string

In this article, you will understand the how to find the frequency of characters in strings by using Java programming language. Along with this, you will understand the hashing concept...

3 minutes read.

Java Comparator Interface

Java comparator interface is used in a situation when we have to sort an object which does not implement Comparable or do sorting in a different way than the Comparable....

1 minute read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 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.

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

Java Garbage Collection Interview Questions

One of the key areas of Java is garbage collection. Garbage collection enables apps to manage memory automatically. Interviewers frequently ask inquiries about garbage collection. Q1: What is the purpose of...

6 minutes read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.