×

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

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

How to Convert Integer to String in Java

How to Convert int to String in Java It is used when you want to convert an integer to String. You can convert int to String by using the following methods: Using...

3 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int. Syntax: public static int toIntExact (long value) Parameters: The...

2 minutes read.

Three Partition Problems in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

8 minutes read.

Inheritance Program in Java

Inheritance Program in Java Inheritance is one of the important pillars of Object-Oriented Programming that facilitates parent-child relationships in programming. Using inheritance, we can create a new class with the help...

7 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

Libraries in Java

The Java Class Library (JCL) specifically is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at any run time, which is fairly significant because...

6 minutes read.

Upcasting in Java

To know what is Upcasting in Java we should first equip ourselves about what is casting or type casting in Java. Casting The process of providing or replacing a reference variable of...

3 minutes read.

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

Java Set to List

In this article, you will be acknowledged about how the process of conversion from Set or HashSet to LinkedList happens and what are the possible ways involved in conversion process. First...

4 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

Trimorphic numbers in Java

Wondered what the Trimorphic number is!! In this article, you can learn about what a Trimorphic number is referred to as and how to find whether a number is trimorphic...

3 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.