Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6797071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:33:48+00:00 2026-05-26T18:33:48+00:00

Is it a good practise to use String#intern() in equals method of the class.

  • 0

Is it a good practise to use String#intern() in equals method of the class. Suppose we have a class:

    public class A {
       private String field;
       private int number;
       @Override
       public boolean equals(Object obj) {
           if (obj == null) {
               return false;
           }
           if (getClass() != obj.getClass()) {
               return false;
           }
           final A other = (A) obj;   
           if ((this.field == null) ? (other.field != null) : !this.field.equals(other.field)) {
               return false;
           }
           if (this.number != other.number) {
               return false;
           }
           return true;
       }
   }

Will it be faster to use field.intern() != other.field.intern() instead of !this.field.equals(other.field).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:33:49+00:00Added an answer on May 26, 2026 at 6:33 pm

    No! Using String.intern() implicitly like this is not a good idea:

    • It will not be faster. As a matter of fact it will be slower due to the use of a hash table in the background. A get() operation in a hash table contains a final equality check, which is what you want to avoid in the first place. Used like this, intern() will be called each and every time you call equals() for your class.

    • String.intern() has a lot of memory/GC implications that you should not implicitly force on users of this class.

    If you want to avoid full blown equality checks when possible, consider the following avenues:

    • If you know that the set of strings is limited and you have repeated equality checks, you can use intern() for the field at object creation, so that any subsequent equality checks will come down to an identity comparison.

    • Use an explicit HashMap or WeakHashMap instead of intern() to avoid storing strings in the GC permanent generation – this was an issue in older JVMs, not sure if it is still a valid concern.

    Keep in mind that if the set of strings is unbounded, you will have memory issues.

    That said, all this sounds like premature optimization to me. String.equals() is pretty fast in the general case, since it compares the string lengths before comparing the strings themselves. Have you profiled your code?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is good practice to use the main method to test a java/.net class?
I have a class Request.cs It has an abstract method: public abstract Response CreateResponse(XmlReader
Adding Exceptional Handling is a good practise, but I have a doubt, In our
Is it good practice to have a class constructor that uses default parameters, or
I have a variable line_number_max_digits and I want to use it in string format
Is it safe/good practice in Perl to use an empty string as false in
Background I have a container class which uses vector<std::string> internally. I have provided a
Looking on my method I'm not sure I have to use three separate try/catch
Basically, I have something like the following: public string SomeDBMethod(string server, string dbName, string
I have this object: Public Cactus{ Public Double key; Public String value; } I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.