Java String intern() Method
What is String?
In Java, an string class is a series of characters that are interpreted literally by a script. In order to preserve a number of components, often letters, a string is represented as a byte (or term) range information structure. Other kinds and configurations of sequence (or array) information can also be designated by the sequence, such as more generalised arrays.
- Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.
To know more about String Class in java read the complete article.
String Intern() Method:
The Java String class intern() method returns the interned string. It gives back the string’s canonical representation. If the string was produced with a new keyword, it can be used to return it from memory. In the String Constant Pool, it duplicates the heap string object exactly.
- The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.
Every distinct String value is only stored once in Java’s string pool. Reusing String objects enables memory savings while running programmes. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
Syntax:
public String intern()
Parameters:
A pool of strings, initially empty, is maintained privately by the class String.
Let’s look at a string-related Java program where the Java String Intern() Method is used to perform an operation on the given string.
Example: The use of intern() method. Run
public class Main { public static void main(String args[]) { String str1 = new String("Hello PrepInsta"); str1 = str1.intern(); String str2 = "Hello"; str2 = str2.intern(); System.out.println(str1.equals(str2)); } }
Output
false
Example 2 : Find the use Intern() Method
public class Main { public static void main(String args[]) { String str1 = new String("Hello"); str1 = str1.intern(); String str2 = "Hello"; str2 = str2.intern(); System.out.println(str1.equals(str2)); } }
Output
true
Example 3: Find the use of Intern() Method.
public class Main { public static void main(String args[]) { String Str1 = new String("Welcome to PrepInsta.com"); String Str2 = new String("WELCOME TO PrepInstaPrime.COM"); System.out.print("Canonical representation:" ); System.out.println(Str1.intern()); System.out.print("Canonical representation:" ); System.out.println(Str2.intern()); } }
Output
Canonical representation:Welcome to PrepInsta.com Canonical representation:WELCOME TO PrepInstaPrime.COM
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment