What is the use of intern() method in java?

What is the use of intern() method in java?

intern() method of String class is used to deal with String duplication problem in Java. Use of this method can save a lot of memories consumed by duplicate String.
A string is duplicate if it contains the same content as another string but stored at different memory locations.
In this case string1 != string2 but string1.equals(string2) is true.
String object consumes a large amount of heap memory and it makes sense to use intern() method to reduce duplication and take advantage of String pool memory feature.
Use intern() method to intern a String object and store them into String pool for further reuse. By calling the intern() method on this String object, we instruct JVM to push this String in the constant pool and whenever someone else creates the same String then this object will be returned instead of creating a new object, in this way we can save a lot of memory in Java, depending upon how many Strings are duplicated in application.

Java Professional with rich experience in Java development.
– Designing and developing high-volume, low-latency applications with high-availability and performance.
– Writing well designed, robust and efficient code.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By : XYZScripts.com