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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:20:55+00:00 2026-06-03T12:20:55+00:00

I have a class with primitive fields like the following: public class Person{ String

  • 0

I have a class with primitive fields like the following:

public class Person{  
  String name;  
  int age;  
  int id;  
  int height;  
  public Person(String name, int age, int id, int height){  
      this.name = name;
      this.age = age;  
      this.id = id;  
      this.height = height;  
  } 
  //other methods
}  

I have various persons that I want to sort by age:

Person p87 = new Person("John", 87, 123, 185);  
Person p22 = new Person("George", 22, 12, 180);  
//etc  ....for other persons. Then I create a list of them
List<Person> persons= Arrays.asList(p87, p22, p45, p31, p55, p62 );  
Collections.sort(persons, new Comparator<Person>() {

            @Override
            public int compare(Person p1, person p2) {
                return p1.age - p2.age;
            }

        });   

Now I want to test if it has been sorted correctly so I have a JUnit test case with the following:

assertEquals(Arrays.asList(p22, p31, p45, p55, p62, p87 ), persons);

The testcase passes. So it is sorted.
But I don’t understand this. The assertEquals would compare the 2 lists using the List.equals which calls the equals on each contained object.
The problem is that I have not override equals in the class Person.
As a result the following fails:

Person p22 = new Person("George", 22, 12, 180);   
Person p22_2 = new Person("George", 22, 12, 180);    
assertEquals(p22, p22_2);  

I don’t understand this. Why then the 2 list passed as equal in the test case?

  • 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-03T12:20:58+00:00Added an answer on June 3, 2026 at 12:20 pm

    In your first example you aren’t creating new objects – you are comparing references to the same object. This works because the default implementation of equals is to compare for reference equality.

    You are doing the equivalent of this:

    Person p22 = new Person("George", 22, 12, 180);   
    Person p22_2 = p22;
    assertEquals(p22, p22_2);   // works, because both refer to the same object
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with some non primitive members. class Relation { String name;
I have a class that contains Append-like members for each primitive type, string and
I have class like below template<class T> class Student { public: static Student& Instance();
I have a Java primitive type at hand: Class<?> c = int.class; // or
Fluent NHibernate doesn't like this, throwing an error: {Association references unmapped class: System.String} OK
Let's say I have a class Foo with some primitive instance variables. I initialize
I have a generic class that should allow any type, primitive or otherwise. The
I have class A: public class ClassA<T> Class B derives from A: public class
I have class Cab(models.Model): name = models.CharField( max_length=20 ) descr = models.CharField( max_length=2000 )
I have a custom class declared as follows (in vb.net) <Serializable()> Public Class NumInfo

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.