Home > Java > javaTutorial > body text

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

高洛峰
Release: 2017-02-03 13:05:38
Original
1651 people have browsed it

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());
  }
} 
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!