Home  >  Article  >  Java  >  Detailed explanation of equals() and toString() method examples in java

Detailed explanation of equals() and toString() method examples in java

高洛峰
高洛峰Original
2017-02-03 13:05:381687browse

equals() and toString() methods in java, here is a small example to help you learn and understand this part of the knowledge.

/*
  所有对象的父类Object
  Object中的方法:
  equals() 对象是否相同的比较方法
  toString()对象的字符串表现形式
*/
class Person
{
  String name;
  int age;
  Person(String name, int age)
  {
    this.name = name;
    this.age = age;
  }
}
class ObjectDemo
{
  public static void main(String[] args)
  {
    Person p = new Person("lixin", 27);
    Person q = new Person("jingxiaoqing", 21);
    if(p.equals(q))
    {
      System.out.println("p = q");
    }else
    {
      System.out.println("nothing");
    }
    System.out.println("p.toString : " + p.toString());
  }
} 

Detailed explanation of equals() and toString() method examples in java

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

For more detailed examples of equals() and toString() methods in java, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn