I have a Java class
class Student
{
String name;
int age;
}
Also, I have two instances of the Student class student1
and student2
.
One way to find out whether both the instances represent the "same student" is to compare the data manually, i.e., the name and age. If they are the same, then they represent the "same student".
Is there any other way to find out whether the two instances represent the "same student"?
P.S. I was asked this question in an interview. I still do not fully understand the phrase "same student". I am guessing it means same student in the real world开发者_StackOverflow社区. If not, what else could the interviewer have meant?
What identifies the student?
We don't have anything but a name and an age. Now, you COULD have two people of the same name, but probably not. In any event, I wouldn't consider age to be something that identifies someone - not like a name does.
So I would do this
public boolean equals(Object o) {
if(!o instanceof Student) return false;
Student s = (Student)o;
return o.name.equals(this.name);
}
In case you're new to Java, the Object class has a method public boolean equals(Object o)
. Because EVERY object in Java inherits from Object, they ALL have that method, whether they want it or not. The default implementation simply compares the address in memory. So if you were to do
Student s = new Student();
Student t = new Student();
boolean e = (s == t); // always false
boolean q = s.equals(t); // always false if not overridden
Now, this particular method that I wrote doesn't do null checks, which you would want to do if it were in production code.
There are two definitions of the same student in Java (if student is an object). There is the case where the student has the same name and age which can be checked by implementing the .equals() method. This means that their values are equal.
@Override
public boolean equals(Object o) {
//check if the student's age and name are equal
}
The second case refers to when two different variables point to the same actual object, this is known as referential equality and can be tested with ==.
if (student1 == student2)
Really good question, Trying my best and hope it will make you understand...
First copying your text Your text = "One way to find out whether both the instances represent the "same student" is to compare the data manually, i.e., the name and age. If they are the same, then they represent the "same student." and "I am guessing it means same student in the real world."
Your first stmt is wrong whereas your guess is right. In a school there are two students of name Frank and both Frank have same age. Does that means that they are same. No off course not. Even if you compare say 5 datas and it could be possible that those 5 data of two different student matches. In real world it is very easy to look at them and say that they are different.
In the language of java "==" double equals refers to "same" and is called the same instance.
If two reference variable student1 and student2 referes to the same instance this means that they are referring to the same student or in the language of java "same instance of student"
In the context of student == is important and suggest you to read .equals() method to understad more about equality.
In my opinion, the easiest way to implement this is to do what most people has said. You can use the inherited method equals
from the Object class ( which all classes inherit from ). If this is for a larger project, I'd also recommend doing something like this:
Assign a UUID to each and every Student
object. This gives every student a uniquely identifiable ID, then you can easily compare those two values in the equals
method. To automatically have it generate a UUID, you can generate a string, based on the hash of all of the attributes, plus some pysudo-random attribute, things like the current time should work. This would prevent overlaps in cases even where two students have the exact same information, because they can't be generated at the exact same time, they'll always have a different UUID.
You could then create a container class, using a HashMap<String, Student>
to store all students, and create method getStudentByID(String id)
and setStudentByID(String id, Student value)
to easily access and manage all students within your collection.
That's how I'd do it, anyways.
If you want to solve it without implementing extra things you could use this snippet below.
package Snippet1;
public class Snippet1 {
static class Student {
public String name;
public int age;
@Override
public boolean equals(Object obj) {
if (obj.getClass().isAssignableFrom(Student.class)) {
Student secondStudent = (Student) obj;
if (secondStudent.age == this.age && secondStudent.name.equalsIgnoreCase(this.name)) {
// Student, with all the same information
return true;
} else {
// Student, but with different information
return false;
}
} else {
// Not a student!
return false;
}
}
}
public static void main(String[] args) {
// Your example ( with little changes )
Student student1 = new Student();
student1.name = "Jane";
student1.age = 15;
Student student2 = new Student();
student1.name = "John";
student2.age = 17;
if (student1.equals(student2)) {
System.out.println("Same student!");
} else {
System.out.println("Different student!");
}
}
}
精彩评论