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()); } }
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!