aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes == and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "== and equals" Watch "== and equals" New topic
Author

== and equals

Daimon Masaru
Greenhorn

Joined: Oct 13, 2008
Posts: 5
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

Joined: Oct 13, 2008
Posts: 5
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

Joined: Jun 06, 2007
Posts: 2712
    
    5

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 ]

SCJP, SCWCD.
|Asking Good Questions|
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1966
    
    7

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

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449


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.


http://muhammadkhojaye.blogspot.com/
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

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

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: == and equals
 
Similar Threads
Strings
Doubt in threads
Equality Comparison Operators
Please Explain (Hashcode)
Object assignments