Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the equals method of Java Object class

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use the Java Object class equals method". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to use the Java Object class equals method" can help you solve the problem.

Basic concepts:

The Object class is located in the java.lang package, and the java.lang package contains the most basic and core classes of Java, which are automatically imported at compile time

The Object class is the ancestor of all Java classes. Each class uses Object as the superclass. All objects, including arrays, implement the methods of this class. You can use a variable of type Object to point to any type of object

Equals () method: compare whether two objects are the same

If two objects have the same type and the same property value, the two objects are said to be equal. If two reference objects refer to the same object, the two variables are said to be the same. The prototype of the equals function defined in the Object class is:

Public boolean equals (Object); he judges whether two objects are the same, not equal

① can only handle reference type variables

In the Object class, ② finds whether the address values of the two reference variables that equals () still compares are equal.

Package com.example.demo.test;public class Person {private String userName; private String age; public String getUserName () {return userName;} public void setUserName (String userName) {this.userName = userName;} public String getAge () {return age;} public void setAge (String age) {this.age = age;}} package com.example.demo.test Public class Test {public static void main (String [] args) {Person p1 = new Person (); Person p2 = new Person (); System.out.println (p1.equals (p2)); System.out.println (p1 = = p2); / * the String class overrides the equals () method of the Object class to compare whether the entity content of the two objects is exactly the same. * / String S1 = new String ("AA"); String S2 = new String ("AA"); System.out.println (s1.equals (S2)); System.out.println (S1 = = S2);}}

What you can see from the running result is that the equals method in Object compares whether two objects are the same.

The equals method in the String class compares whether the value of a string is equal. Please see the method of equals in String.java

Public boolean equals (Object anObject) {if (this = = anObject) {return true;} if (anObject instanceof String) {String anotherString = (String) anObject; int n = value.length; if (n = = anotherString.value.length) {char v1 [] = value; char v2 [] = anotherString.value Int I = 0; while (nMurray -! = 0) {if (v1 [I]! = v2 [I]) return false; iTunes;} return true;}} return false } this is the end of the introduction to "how to use the equals method of the Java Object class". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report