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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:04:16+00:00 2026-05-14T14:04:16+00:00

public class abc1 { private String s; public abc1(String s){this.s=s;} public static void main(String

  • 0
public class abc1 {

 private String s;

 public abc1(String s){this.s=s;}
 public static void main(String args[])
 {
  HashSet<Object> hs=new HashSet<Object>();
  abc1 a1= new abc1("abc");
  abc1 a2= new abc1("abc");
  String s1= new String("abc");
  String s2= new String("abc");
  hs.add(a1);
  hs.add(a2);
  hs.add(s1);
  hs.add(s2);
  System.out.println(hs.size());

 }
}

Why above program output is 3?

Edit

Seeing below comments I am extending my question:

System.out.println (s1 == s2);

Are s1 and s2 refering to same object? If then the above statement should print true but its output is false.

Are they are similar in terms of hashcode but still differnt?

  • 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-14T14:04:17+00:00Added an answer on May 14, 2026 at 2:04 pm

    There are two unequal instances of abc1 (note that it doesn’t override equals or hashCode) and one string in the set. Let’s look at the four add calls:

    hs.add(a1);
    

    Originally the set is empty – so obviously this will add the value.

    hs.add(a2);
    

    This will also add the value to the set, because they are distinct objects and the default implementation of equals/hashCode is basically reference identity.

    hs.add(s1);
    

    This will add the value to the set as the string isn’t equal to either of the current values (which aren’t strings).

    hs.add(s2);
    

    This will not add anything to the set, as the second string is equal to the first. (String overrides equals/hashCode.)

    The result is a set with three items in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
public class Empty { public static void main( String[] args ) { TreeSet<Class> classes
public class Asterisk { public static void main(String[] args) { String output=""; int count=1,
public class EventsType { public event EventHandler> NewEvent; public void SmthHappened(string data) { MyEventArgs<Object>
public class CovariantTest { public A getObj() { return new A(); } public static
public class WrapperTest { static { print(10); } static void print(int x) { System.out.println(x);
In the following snippet: public class a { public void otherMethod(){} public void doStuff(String
public class RichTextBoxExtended : RichTextBox { static RichTextBoxExtended() { //DefaultStyleKeyProperty.OverrideMetadata(typeof(RichTextBoxExtended), new FrameworkPropertyMetadata(typeof(RichTextBoxExtended))); } public

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.