2

In one of my interviews, an interviewer asked me:

Given a Student class and two objects s1 and s2:

s1 = new Student();
s2 = new Student();

How will s1 == s2 return true?

I told him to make Student class a singleton, but he said no, and that we have to make a change in the class level so that s1 == s2 would return true.

Note: we need to change Student class. Please don't reply s1=s2. Any clue?

Eran
  • 387,369
  • 54
  • 702
  • 768
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30

6 Answers6

5

The operator == check if two objects are the same. You created two different equals object. So they are not the same and s1 == s2 will return false. You have to redefine the method equals and check them with this method as follow:

s1.equals(s2)

The method equals:

Indicates whether some other object is "equal to" this one.

Note that when you redefine the method equals you need also to redefine the method hashCode, as explicitly documentated in the description of equals method:

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Generally ide (like IntelliJ, Eclipse or Netbeans) helps you writing a good implementation of both methods.

Considering this I suppose that the interviewer has asked something like How will s1 equals s2 talking about it and you misunderstood it as How will s1 (simble equals) s2. Or he has explicitly written the operator == on the paper?


If the interviewer asked explicitly how to will

s1 == s2 // returns true

after creating the two objects as

Student s1 = new Student();
Student s2 = new Student();

The only possibility is to change the reference of s1 (or s2) as follow:

Student s1 = new Student();
Student s2 = new Student();

s1 = s2;  // new added line , or the same if you write s2 == s1

s1 == s2  // now is true

But this is a trick, infact you are testing that two different variables are referencing the same object.

You can have a similar behaviour assigning to both variables null, or another Student previously created. Basically any change to code that assign to s1 the same reference of s2 will work.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • 1
    I think everyone is taking it as 'equals' but it was clearly '==' – Deepu--Java May 31 '18 at 13:19
  • 1
    probably your interviewer meant to ask about equals() but made a mistake. don’t give just short literal answers to interview questions. use it as a chance to explain what you know, your reasoning and problem solving skills. – Patrick Parker May 31 '18 at 13:27
  • Yes, Its was == thats why I replied Singleton but he wanted me to change in Student class. – Deepu--Java May 31 '18 at 13:28
  • 5
    You can't create instances of objects using the operator new and having the check with == returning true on different instances – Davide Lorenzo MARINO May 31 '18 at 13:29
  • to put it simply, if u used a new on a class then that results in an object construction on the heap. And if we are comparing two references using == which are like comparing two different memory locations on heap. It has to definitely return false. Singletons usually make constructors private, and expose a construction method that return the same object reference with each call , if it had created once. – JineshEP May 31 '18 at 14:46
3

This is kind of a trick, but will meet the requirements:

Change the Student constructor to throw some exception (I chose an unchecked exception, so I don't have to specify it in a throws clause):

public Student()
{
    throw new NullPointerException();
}

Now, assuming we are allowed to add a try-catch block:

Student s1 = null;
Student s2 = null;
try {
    s1 = new Student(); 
    s2 = new Student();
}
catch (Exception e) {
}
System.out.println (s1==s2);

This will print true, since both s1 and s2 are null.

Even if we don't catch the exception, s1 == s2 will still be true after the two constructor calls (actually after the first constructor call, since the second one will never be executed), but we have to catch the exception somewhere in order to test it.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • @DeepakTiwari well, if you are allowed to add a try-catch block you can simply add `s1 = s2` and be done. Accepting this answer really makes zero sense. It is the most complex way of achieving something you did not make the requirements clear for. – luk2302 Jun 01 '18 at 12:43
  • Instead of `try ... catch` you can write `if (false) ...` and achieve the same result. – luk2302 Jun 01 '18 at 12:48
  • @luk2302 can you do any other thing in Student class except above given solution to achieve this? – Deepu--Java Jun 01 '18 at 13:51
  • @luk2302 till now I accepted this and found some bit relevant. If I get some other answer better than this, I will change this for sure. – Deepu--Java Jun 01 '18 at 13:53
  • @DeepakTiwari you stated that you are supposed to change something in the `Student` class, this answer does MUCH more than that, it changes _something_ outside the class, if you are allowed to change _something_ outside of it there is nothing preventing you from doing either `if (false) ...` or `s1 = s2;`. The main takeaway is that your requirement is really, really broad and very bad and that either you or the interviewer did not understand the problem at hand. – luk2302 Jun 01 '18 at 14:09
  • @luk2302 that would certainly be cheating, since no code of the Student class will be executed at all in your suggestion. – Eran Jun 01 '18 at 14:10
  • In the `if (false) ...` case that is true, but since you added a try-catch you can easily simply add a single line after the two constructor calls stating `s1 = s2;` and be done with it, the calls to Student() will be run (even one more than in your example) and still it will evaluate to true, yes it is useless, but not more useless than the original task. – luk2302 Jun 01 '18 at 14:13
  • @luk2302 I only made a change outside the class in order to be able to evaluate s1==s2 to show that it's true. If I kept the original two lines of code, my answer would still result in s1==s2 being true. – Eran Jun 01 '18 at 14:13
  • Only if you assign `null` to them initially which was not part of the problem description. – luk2302 Jun 01 '18 at 14:20
  • @luk2302 well, if they are instance variables, they will be assigned `null` by default. If they are uninitialized local variables, they won't have any value, so either the `s1==s2` comparison won't be executed (if you don't include the try-catch block) or the code won't pass compilation (if you include the try-catch block and perform the comparison after it). – Eran Jun 03 '18 at 12:18
2

As already answered here, == compares the reference. ie it compares if s1 and s2 are pointing to the same object. Since you are using new to instantiate both s1 and s2, your requirement is just impossible.

vincrichaud
  • 2,218
  • 17
  • 34
1

The only logical solution I see are trivial:

s1 = new Student();
s2 = new Student();
s1=null;
s2=null;
System.out.println(s1==s2);

or:

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

or:

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

as @user7 suggested in the comment

0

== compares object references, it checks to see if the two operands point to the same object (not equivalent objects, the same object). So I really think that the only way can be something like this:

@Override
public Student clone(){
    return this;
}

Maybe this will make the == operator work as you asked. This is horribly wrong to me because I don't think that this is the intended use of clone() method. Otherwise I do not have any other clues on how to make as you asked considering the constraint of working at class level.

If clone() cannot be used maybe the answer will be: it is not possible to do that.

Lorelorelore
  • 3,335
  • 8
  • 29
  • 40
-3

As == operator compares objects references, I think that s1 and s2 has to be null.

WizardNx
  • 697
  • 5
  • 10