EDIT: I'm new to java and am going through the official documentation in preparation for an upcoming subject in my Computer Science studies.
Original question:
So I checked this thread re: java == and .equals and am getting some unexpected results in dummy code that I'm writing.
I have this:
public class HelloWorld {
public static void main(String[] args){
Double double1 = 1.0; //object 1
Double double2 = 1.0; //object 2, which should be a different memory address?
System.out.println(double1.equals(double2)); //compare using the Object.equals() method
System.out.println(double1 == double2); //compare with ==
}
}
//Results are:
//true
//false
As expected, == produces a false because the two objects refer to different memory addresses.
However the double.equals(double2) is producing a true, which is not what I expected. In this example, I'm not overriding the default .equals() inherited from Object. My understanding is that equals checks for reference equality as well as values.
So why does double1.equals(double2) return true instead of false in this example? Are't double1 and double2 referring to two difference objects and therefore equals() should return false?