This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line!
See this thread for details.
Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

a question about equals() method?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class MyParm{
public static void main(String argv[]){
Object m1 = new Object();
Object m2 = new Object();
System.out.println(m1);
System.out.println(m2);
if (m1.equals(m2)){
System.out.println("Equals");
}else{
System.out.println("Not Equals");
}
}
}
-------------------------------------------------------
the result of the up code is "Not Equals", the equals() is a method of a object to compare the content between two objects,
so, there has create two objects from Object class in the same way, why not the two object have the same content?
pls help me, thank you.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David, check the API to see how the Object class defines the equals() method.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to API:
"The equals method for class Object ... is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true)."
Object m1 = new Object();
Object m2 = new Object();
Although m1, m2 created in the same way, they are not the same object.
 
David chenjl
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you, Li Yi and Scott,
So,Can I use the operator "==" to desplace the equals() to compare anything?,the == only compares the memory address that i get from turial, how can i compare two object, by equals() or another?
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David, in your own classes you can redefine the equals() method to compare values instead of object references (as the String class has done) or to compare 2 objects by any criteria you choose. There is a bit of complexity to this, however. (Check out Comparable/Comparator in the API and in the tutorial about Collections).
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java does not support operator overloading so you can't do anything with ==
 
We're all out of roofs. But we still have tiny ads:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic