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

  • Home
  • SEARCH
  • 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 6076405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:36:13+00:00 2026-05-23T10:36:13+00:00

Can someone please explain why hashCode is called in the example below? import java.util.List;

  • 0

Can someone please explain why hashCode is called in the example below?

import java.util.List;

public class JSSTest extends Object{

    public static void main(String args[]){

        JSSTest a = new JSSTest();
        JSSTest b = new JSSTest();
        List<JSSTest> list = new java.util.ArrayList<JSSTest>();
        list.add(a);
        list.add(b);
        System.out.println(list.get(0));
        System.out.println(list.get(1));
    }

    @Override
    public boolean equals(Object obj){
        System.out.println("equals");
        return false;
    }

    @Override
    public int hashCode(){
        System.out.println("hashCode");
        return super.hashCode();
    }
}

Outcome:

hashCode 0
JSSTest@1bab50a
hashCode 0
JSSTest@c3c749
  • 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-23T10:36:14+00:00Added an answer on May 23, 2026 at 10:36 am

    The default toString() implementation calls hashCode. This has nothing to do with lists.

    Here’s a fairly minimal repro:

    public class JSSTest {
    
        public static void main(String args[]){
            JSSTest test = new JSSTest();
            // Just to show it's not part of creation...
            System.out.println("After object creation");
            test.toString();
        }
    
        @Override
        public boolean equals(Object obj){
            System.out.println("equals");
            return false;
        }
    
        @Override
        public int hashCode(){
            System.out.println("hashCode");
            return super.hashCode();
        }
    }
    

    (You could override toString() to display before / super call / after details, too.)

    It’s documented in Object.toString():

    The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

    getClass().getName() + '@' + Integer.toHexString(hashCode())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain the following code? Source: Arrays.class, public static <T> void sort(T[]
Can someone please explain how JAR files and the Java class loader make use
Can someone please explain why is meta class used in the following example. Ex:
Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Can someone please explain what the & does in the following: class TEST {
can someone please explain to me why having a doctype of <!DOCTYPE HTML PUBLIC
Can someone please explain why this test fails: [Fact] public void ResolveAllDoesNotReturnServicesRegisteredInParent() { //
Can someone please explain where GetModuleCatalog (Microsoft.Practices.Composite.UnityExtensions.UnityBootstrapper.GetModuleCatalog()) is called in the Prism shell? I
Can someone please explain the below to me. First is how I call the
Can someone please explain why pointers aren't initialized to NULL ? Example: void test(){

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.