Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the equals method of Java Object class

Shulou Source: shulou.com Published: 2022-05-31 22:44:23 10月17日 Update

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.

Tags: Methods two objects variables types same knowledge content industry different practical functions prototypes addresses foundations characters strings entities practicality reality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi Xiaomi MariaDB macOS