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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:14:08+00:00 2026-06-13T07:14:08+00:00

Object o1 = new Object(); Object o2 = new Object(); //o1=o2; System.out.println(o1.equals(o2)); It returns

  • 0
Object o1 = new Object();
Object o2 = new Object();
//o1=o2;
System.out.println(o1.equals(o2));

It returns false. It can return true, if the comment is removed.


Why isn’t the same thing applicable to the String class?

String s1=new String();
String s2=new String();
System.out.println(s1.equals(s2));

It returns true. Why? (because String uses interns or something else involved?)

  • 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-13T07:14:09+00:00Added an answer on June 13, 2026 at 7:14 am

    Because equals() for String compares the content, not the object itself.

    public boolean equals(Object anObject)

    Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

        /* String.equals() */
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = count;
            if (n == anotherString.count) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = offset;
                int j = anotherString.offset;
                while (n-- != 0) {
                    if (v1[i++] != v2[j++])
                        return false;
                }
                return true;
            }
        }
        return false;
    }
    

    (Link to the source of String.equals())

    Versus the equals for Object:

    The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

    /* Object.equals() */
    public boolean equals(Object obj) {
        return (this == obj);
    }
    

    (Link to the source of Object.equals())

    Also, don’t forget the contract of the equals() function:

    The equals method implements an equivalence relation on non-null object references:

    • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
    • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
    • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
    • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
    • For any non-null reference value x, x.equals(null) should return false.

    Also recommended reading:

    • Object.hashCode()
    • Effective Java (Bloch)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this
String a []= {null,null,null,null,null}; //add array to arraylist ArrayList<Object> choice = new ArrayList<Object>(Arrays.asList(a)); System.out.println(choice.size());
I'm trying to create a new System.Threading.Thread object using Jscript, but I can't get
$doc = New-Object System.Xml.XmlDocument $doc.Load(http://nykdev101.us.net.intra/carlos/amoncalltracker-temp-3-2011.nsf/%28XMLView%29?OpenAgent&view=%28StaffDetailsByRotation%29) $xml = [xml]($doc) $xml.Save(C:/TEMP/data.xml) what I wrote is just
protected void Button1_Click(object sender, EventArgs e) { System.Collections.ArrayList list = new System.Collections.ArrayList(); list.Add(abc); list.Add(xyz);
I have the following Powershell code: Add-Type -Assembly System.Configuration [ExeConfigurationFileMap] $configMap = New-Object ExeConfigurationFileMap
The doc about java.util.Set.contains(Object o) says: Returns true if and only if this set
I may be wrong but for me, we can override equals for an object
object[] objs = new object[]{one,two,three}; Are the strings stored in the array as references
object[] objArray = new object[]{blah, 4, whatever}; foreach(var value in objArray) vs. foreach(object value

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.