🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
String.intern() method in Java is used to return a canonical representation of the string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
internMethod Syntax- Examples
- Interning a String
- Comparing Interned Strings
- Benefits of String Interning
- Conclusion
Introduction
The String.intern() method is a member of the String class in Java. It allows you to ensure that all identical strings are represented by the same String object, which can help save memory and improve performance in some cases.
intern Method Syntax
The syntax for the intern method is as follows:
public native String intern();
Examples
Interning a String
The intern method can be used to return a canonical representation of a string.
Example
public class InternExample {
public static void main(String[] args) {
String str1 = new String("Hello");
String str2 = new String("Hello");
String internedStr1 = str1.intern();
String internedStr2 = str2.intern();
System.out.println("str1 == str2: " + (str1 == str2)); // false
System.out.println("internedStr1 == internedStr2: " + (internedStr1 == internedStr2)); // true
}
}
Output:
str1 == str2: false
internedStr1 == internedStr2: true
Comparing Interned Strings
Interning strings ensures that identical strings share the same memory reference, making it easy to compare strings using the == operator.
Example
public class InternExample {
public static void main(String[] args) {
String str1 = "Java";
String str2 = new String("Java");
String internedStr2 = str2.intern();
System.out.println("str1 == str2: " + (str1 == str2)); // false
System.out.println("str1 == internedStr2: " + (str1 == internedStr2)); // true
}
}
Output:
str1 == str2: false
str1 == internedStr2: true
Benefits of String Interning
String interning can be beneficial in situations where you have many identical strings, as it reduces memory usage by ensuring that only one instance of each unique string is stored.
Example
public class InternExample {
public static void main(String[] args) {
String[] strings = new String[1000];
for (int i = 0; i < strings.length; i++) {
strings[i] = ("String" + i % 10).intern();
}
System.out.println("Memory saved by interning strings.");
}
}
Output:
Memory saved by interning strings.
Conclusion
The String.intern() method in Java is used for optimizing memory usage and performance by ensuring that identical strings are represented by the same String object. By understanding how to use this method, you can efficiently manage strings in your Java applications. Whether you are interning strings, comparing interned strings, or taking advantage of the benefits of string interning, the intern method provides a reliable solution for these tasks.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment