Java Foundation 03-12_ Object Comparison

Source: Internet
Author: User

Object comparison

If you now have two numbers to determine whether they are equal, you can use "= =" to complete

If the string is to be judged for equality use "equals ()"

But if there is a custom class Now, to determine whether its two objects are equal, then it is necessary to implement a comparison of all the property contents within the Class object.

Example: How the underlying comparison works
class Book {    private String title;    private double price;    public Book(String title, double price) {        this.title = title;        this.price = price;    }    public String getTitle() {        return this.title;    }    public double getPrice() {        return this.price;    }}public class TestDemo {    public static void main(String args[]) {        Book b1 = new Book("Java开发", 79.8);// 实例化Book类对象        Book b2 = new Book("Java开发", 79.8);// 实例化Book类对象        if (b1.getTitle().equals(b2.getTitle())&& b1.getPrice() == b2.getPrice()) // 属性比较            System.out.println("是同一个对象!");        } else {            System.out.println("不是同一个对象!");        }    }}
Example: Object comparison implementation
class Book {    private String title ;    private double price ;    public Book(String title,double price) {        this.title = title ;        this.price = price ;    }    /**     * 进行本类对象的比较操作,在比较过程中首先会判断传入的对象是否为null,而后判断地址是否相同,     * 如果都不相同则进行对象内容的判断,由于compare()方法接收了本类引用,所以可以直接访问私有属性     * @param book 要进行判断的数据     * @return 内存地址相同、或者属性完全相同返回true,否则返回false     */    public boolean compare(Book book) {        if (book == null) {     // 传入数据为null            return false ;      // 没有必要进行具体的判断        }        // 执行“b1.compare(b2)”代码时会有两个对象:当前对象this(调用方法对象,就是b1引用); 传递的对象book(引用传递,就是b2引用)        if (this == book) {     // 内存地址相同            return true ;       // 避免进行具体细节的比较,节约时间        }        if (this.title.equals(book.title)            && this.price == book.price) {  // 属性判断            return true ;        } else {    return false ;  }    }// setter、getter略}

Java Foundation 03-12_ Object Comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.