Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== and equals

 
Daimon Masaru
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Daimon Masaru
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Can someone correct me?
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



Remember to overide Hash Function as well when implementing the equal method for Toy.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put together a little mock certification exam to address just this very confusion. It's a very common topic for SCJA candidates to get tripped up on.

Check out this little SCJA Mock Exam on equals, == and =.

SCJA Mock Certification Exam on Equals Objectives

Good luck!

-Cameron McKenzie

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic