
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What Does intern() Method in Java Do?
The intern() method of the String method returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String.
For any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are interned.
Example
import java.lang.*; public class StringDemo { public static void main(String[] args) { String str1 = "This is TutorialsPoint"; // returns canonical representation for the string object String str2 = str1.intern(); // prints the string str2 System.out.println(str2); // check if str1 and str2 are equal or not System.out.println("Is str1 equal to str2 ? = " + (str1 == str2)); } }
Output
This is TutorialsPoint Is str1 equal to str2 ? = true
Advertisements