In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the comparison of java objects". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the comparison of java objects".
Catalogue
1. Comparison of elements
2. Comparison of classes
3. Comparison method
3.1 override the equals method
3.2 comparison based on Comparble interface classes
3.3 comparator-based comparison: Comparator interface
3.4 comparison of three comparison methods
1. Comparison of elements
In java, basic types of objects can be directly compared in size.
Public static void main (String [] args) {int axi12; int bread55; System.out.println (a > b); System.out.println (a = = b); System.out.println (ach3); System.out.println (ch2==ch3); System.out.println (ch2c2); compile error System.out.println (c1==c2) / / print false, because C1 and c2 point to different objects / / System.out.println (C1 or c2
< 方式进行比较,但确可以使用==符号进行比较? 原因:对于用户自己实现定义的类型,都是默认为Object类,Object类提供了equal方法, 此方法的比较规则是: 无比较引用变量引用对象的内容,而是直接比较引用变量的地址,因此equal方法一般在使用的时候会被重写 // Object中equal的实现,可以看到:直接比较的是两个引用变量的地址public boolean equals(Object obj) { return (this == obj); }3、比较方法 在一些情况下,我们需要比较的是对象的内容,而不是比较对象的地址是否相同,则需要重写比较方法。 3.1 重写equals方法class Card{ public int rank; public String suit; public Card(int rank, String suit) { this.rank = rank; this.suit = suit; } @Override public boolean equals(Object o) { if (this == o) return true; // o如果是null对象,或者o不是Card的子类 if (o == null || !(o instanceof Card)) return false; Card card = (Card) o; return rank == card.rank && Objects.equals(suit, card.suit); }}Public class Main{public static void main(String[] args){ Card c1 = new Card(1, "♠"); Card c2 = new Card(1, "♠"); System.out.println(c1.equals(c2));}} 其equal方法实现如下: 如果这个类指向同一个对象则直接返回true; 如果传入的为空或者其对象类型不是Card,则返回false 按照类的实现目标完成比较,这里只要花色和数值一样,就认为是相同的牌 注意下调用其他引用类型的比较也需要 equals,例如这里的 suit 的比较 3.2 基于Comparble接口类的比较 Comparble是JDK提供的泛型的比较接口类,源码实现具体如下: public interface Comparable{// 返回值:// < 0: 表示 this 指向的对象小于 o 指向的对象// == 0: 表示 this 指向的对象等于 o 指向的对象// >0: indicates that the object pointed to by this is equal to the object public int compareTo (To) pointed to by o;}
For user-defined types, if you compare by size and manner: you can implement the interface Comparable when defining the class and override the compareTo method in the class.
Class Card implements Comparable {public int rank; public String suit; public Card (int rank, String suit) {this.rank = rank; this.suit = suit;} @ Override public int compareTo (Card o) {return this.rank-o.rank;} @ Override public boolean equals (Object o) {if (this = = o) return true; if (o = = null | |! (o instanceof Card)) return false Card card = (Card) o; return rank = = card.rank & & Objects.equals (suit, card.suit);}} public class Main {public static void main (String [] args) {Card p = new Card (1, "♠"); Card Q = new Card (2, "♠"); Card o = new Card (1, "♠"); System.out.println (p.compareTo (o)) / / = 0, the sign is equal to System.out.println (p.compareTo (Q)); / /
< 0,表示 p 比较小 System.out.println(q.compareTo(p));// >0, indicating that Q is larger}}
3.3 comparator-based comparison: Comparator interface
Compare in the way of the comparator, the specific steps are as follows:
User-defined comparator class to implement Comparator interface
Override the compare method in Comparator
Class Card {public int rank; public String suit; public Card (int rank, String suit) {this.rank = rank; this.suit = suit;} @ Override public boolean equals (Object o) {if (this = = o) return true; if (o = = null | |! (o instanceof Card)) return false; Card card = (Card) o Return rank = = card.rank & & Objects.equals (suit, card.suit);}} class CardComparator implements Comparator {@ Override / / return value: / /
< 0: 表示 o1 指向的对象小于 o2 指向的对象 // == 0: 表示 o1 指向的对象等于 o2 指向的对象 // >0: indicates that the object pointed to by o1 is equal to the object public int compare (Card o1, Card O2) {if (o1 = = O2) {return 0;} / / determine whether o1 is empty if (o1 = = null) {return-1;} / / determine whether O2 is empty if (O2 = = null) {return 1 } return o1.rank-o2.Rank;}} public class Main {public static void main (String [] args) {Card p = new Card (1, "♠"); Card Q = new Card (2, "♠"); Card o = new Card (1, "♠"); / / define the comparator object CardComparator cmptor = new CardComparator () / / use the comparator object to compare / / = = 0, and the signage is equal to System.out.println (cmptor.compare (p, o)); / /
< 0,表示 p 比较小 System.out.println(cmptor.compare(p, q)); // >0, indicating that Q is larger than System.out.println (cmptor.compare (Q, p));}}
3.4 comparison of three comparison methods
Thank you for your reading, the above is the content of "how to understand the comparison of java objects". After the study of this article, I believe you have a deeper understanding of how to understand the comparison of java objects, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.