• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

== and equals

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic