The intern method returns the canonical form of the string, can it be different or the same as the string itself. Is it helpful in boosting the performance or just removes uncertainty?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
intern() improves performance by reducing the number of duplicate strings. This reduces memory consumption but more importantly improve use of the caches. (The caches being much smaller than you main memory)
However, using it directly can have performance problems if you place too many string in this pool (not something a normal program would do, but you could write a program to do so) This is because it is expensive to add strings to the pool O(N) not O(log N) as you might assume and even more expensive to clean up. In java 7 the string literal pool was moved to the heap to reduce the impact of large String literal pools but I would still suggest you use it sparingly if at all.