Okay, so I read that == compares memory locations and .equals compares the actual values for object.
And I chance upon this snippet of code my friend asked me about
Okay, whats up with this, the answer given to us is there is NO output.
[ October 13, 2008: Message edited by: a abc ]
Joined: Oct 13, 2008
Nevermind, I think I got the answer.
== compares memory location of the variable, .equals compares objects and their own value.
new keyword creates a new memory location for your object.
If you use .equals in your own made class, you must create a method to override it before you can use it properly, but I suppose primitive types with their own Wrapper class and String objects have their own .equals method?
When you declare a 2 String or any other primitive types of the same value, the Java Virtual Machine will automatically search for the same value that is previously declared and make a reference to it.
However, when you use the new keyword, a new memory location is created even for String types.
Hi a abc welcome to Javaranch , your name does not follow the Javaranch naming policy . Please change it using the My Profile link above. Also make your subject line more descriptive. Read this to know why. [ October 13, 2008: Message edited by: Amit Ghorpade ]
Welcome to the Ranch Daimon. You got half of it right in my opinion.
In your code there is not equals() method (eg not overridden). So it uses java.lang.Object's equals method which is the same as saying ==. Now since boy and girl are created using "new Toy()" so their physical memory location is different. Hence no output.
Remember for Strings it's different.
If you implement the equals method for Toy then it will return true by comparing the Brand and/or Type String variables. But that's up to you to do