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 7987847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:15:54+00:00 2026-06-04T12:15:54+00:00

Fast question I am comparing a String, should I use equals or compareTo? because

  • 0

Fast question

I am comparing a String, should I use equals or compareTo?
because I though equals distinguish 2 objects of type String
and not just their value…

which may cause problems since:

String a = new String("lol");
String b = new String("lol");

are two different objects even if they have the same value?

Whats exactly difference between equals and compareTo implementations in terms of performance and precision?

  • 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-06-04T12:15:56+00:00Added an answer on June 4, 2026 at 12:15 pm

    First of all == compares the references to see if the two objects are the same (so == is on the object).

    Then String.equals() verify the equality of the content of two strings while String.compareTo() seek the difference of the content of two strings.

    So the two following tests are equivalent:

    String str = "my string";
    
    if ( str.equals("my second string")) {/*...*/}
    if ( str.compareTo("my second string")==0) {/*...*/}
    

    But, since String.equals is making a reference check first, it’s safe when used against null, while String.compareTo will throws a NullPointerException:

    String str = "my string";
    
    if ( str.equals(null)) {/* false */}
    if ( str.compareTo(null) {/* NullPointerException */}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi Just a fast question. I am trying to use System.ComponentModel.DataAnnotations in .NET to
Well fast question is there any way to use a css class in a
This is mostly a theoretical question I'm just very curious about. (I'm not trying
Simple question, if your iterating through a list of objects using fast enumeration is
Just a fast and simple question: how do I add multiple classes to the
Question Edited : HashSet and HashMap are fail-fast(but that is not gauranteed) as mentioned
I recenty asked a question about parallel programming algorithms which was closed quite fast
I've already looked at questions git rebase and git push: non-fast forward, why use?
Simple and fast question, i have those tables: //table people | pe_id | pe_name
Thanks to this question/answer Automatic Reference Counting: Error with fast enumeration I resolved a

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.