×

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

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long. Syntax: public static int subtractExact (int x,...

1 minute read.

Difference between JDK, JRE and JVM In Java

In the Java ecosystem, three primary components are often mentioned: JDK, JRE, and JVM. Here’s a breakdown of each: Java Development Kit (JDK) The Java Development Kit (JDK) is a comprehensive software...

2 minutes read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Balanced Parentheses in Java

One of the frequent programming issues, commonly referred to as the Balanced brackets issue, is the problem with the balanced parenthesis. Interviewers frequently provide this task, in which we must...

5 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.