I have been told to never use == for strings but for everything else because .equals would compare the values rather than the instances of the object. (Which I understand the difference of).
According to some sites, == compares memory locations?
What I don’t understand is if you’re comparing an integer with another, why would it compare memory locations, or is this just for strings?
If you’re comparing int 3 to int 4 obviously it wouldn’t be in the same memory location, but then if you’re comparing int 4 to int 4, does that mean all integers with the value of 4 is stored in the same memory location?
== compares the values of the oparands whether it is primitive or reference type.
If the operands are primitive the values of the operands will be compared.
Operands which are references contains values i.e. address to access the object they are referring to. String are not primitive data type, they are considered as objects in java, When you are comparing two references of type string, the result will be true only when the values of the operands i.e. address of the String objects are equal ( which means they refer to the same String object).