×

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

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.

Java Math sin() Method

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

1 minute read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

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

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.