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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:27:58+00:00 2026-06-15T16:27:58+00:00

It is question from OCJP 6 exam, so it is intentionally not fully correct

  • 0

It is question from OCJP 6 exam, so it is intentionally not fully correct (but legal).

Given code:

class ToDos {
    String day;

    public ToDos(String d) {
        day = d;
    }

    public boolean equals(Object o) {
        return ((ToDos) o).day == day;
    }

    public int hashCode() { return 9; }
}

public class MapEQ {

    public static void main(String[] args) {
        Map<ToDos, String> map = new HashMap<ToDos, String>();

        ToDos t1 = new ToDos("Monday");
        ToDos t2 = new ToDos("Mond" + "a" + "y");
        ToDos t3 = new ToDos("Tuesday");

        map.put(t1, "a");
        map.put(t2, "b");
        map.put(t3, "c");

        System.out.println(map.size());
    }

}

Why output is 2? I mean, equals method is not overriden corretcly, so t1.day == t2.day supposed to be false! Am I’m missed something?

  • 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-15T16:27:59+00:00Added an answer on June 15, 2026 at 4:27 pm

    It’s because:

    "Mond" + "a" + "y"
    

    is evaluated at compile time to “Monday” thus resulting in just one instance in string pool. There is a much simpler test to examine this behavior:

    System.out.println("Mond" + "a" + "y" == "Monday");  //true
    
    String y = "y";
    System.out.println("Mond" + "a" + y == "Monday");  //false
    
    final String y2 = "y";
    System.out.println("Mond" + "a" + y2 == "Monday");  //true
    

    Examples above should give you some overview on how the compiler treats string concatenation.

    And to be on the safe side, always use:

    return ((ToDos) o).day.equals(day);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question from an exam: Given a linked list with unknown size n , where
Question from beginner. Why code <%= System.DateTime.Now.ToLongDateString() %> is not executed in ASP.NET. Could
This is a question from recent GATE entrance exam. A process executes the code
I tried several question from this forum but could not get a GOOD tutorial
Question from the one interview. Please explain what does this C++ code mean: void
Followup question from this one: Swing font names do not match? (Making a font
The question from the title in code: @Transactional (readonly = true) public interface FooService
Question from certification exam: You use Microsoft Visual Studio 2010 and Microsoft .NET Framework
The full question from our sample exam paper: Explain by highlighting the relevant parts
Another newbie question from me. Just checking whether my code below is possible. I.e

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.