I'm a newbie to Java. Now I'm studying equals and == and redefinition of equals and toString.
I would like to use both the toString method that I have redefied and the default method that is inherited from the Object class.
I failed to use that super modificator to reach that method.
This is for educational purposes only. What I would like to get is more clear if you will have a look at the comments in my code.
Could you help me here?
My code is:
public class EqualTest{
public static void main(String[] args){
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
//System.out.super.println(alice1);
Employee alice2 = alice1;
//System.out.super.println(alice2);
Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
//System.out.super.println(alice3);
System.out.println("alice1==alice2: " + (alice1==alice2));
System.out.println("alice1 == alice3: " + (alice1==alice3));
System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
}
}
class Employee{
...
public String toString(){
return getClass().getName() + "[name = " + name +
", salary=" + salary + ", hireDay=" + hireDay + "]";
}
}
toString().
But I don't see any requirement here to actually get the address at all, just the original result oftoString(),
which is available viasuper.toString()
, despite your unspecified 'failure'. Your actual question remains unclear. – user207421It is the address
is plain wrong. It may be the address (and in most cases, but not all, it probably is). Ie, if you rely onhashCode()
for getting the address of objects it may work in some cases, but in other cases it may fail. Eg, it'd be possible according to the specs that some JVM keeps a completely random number as hashcode stored for each object. In reality, this probably wouldn't be the case, since it would entail unecessary storage overhead. Anyways, in Java you typically shouldn't be concerned with at what memory addresses objects are located. Java is not C. – Alderath