Make writing a habit together! This is the 15th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Hello, everyone. I’m Meow hand.

Today, I would like to share some knowledge points I have learned daily with you, and communicate and learn with you in the form of words. Only by learning from each other can we grow faster, right?

I am a Java development, so the daily contact with the most is Java, so I take advantage of their free time, to have a good memory, to learn their own will, output, not what return, just want to help more friends, good.

Friends in the process of reviewing, if you think the article is good, welcome to like, collect, pay attention to oh. Three even is the best encouragement and support for meow hand on my creation road!

One, foreword

Today we are going to introduce you to the Object class. After all, the java.lang.Object package is often used when you are actually developing it. It provides methods like toString(), equals(), etc. If you don’t use it, it’s impossible. It’s all part of backend development.

So today I’m going to tell you about this class. I want to focus on the following three aspects:

  • Object class features
  • ToString () method of the Object class
  • Equals () of the Object class

If you read my content, then you can say that the above three points if you master, then you can say that the basic mastery of Object class, ha ha ha, as for skilled use, must be to scratch the source code.

Okay, I’m going to start teaching, so listen up.

Second, the Object class features

The Object class is the root of the Java language. It is the parent class of all classes. All methods in the Object class can be used by subclasses. When you instantiate an Object, the ultimate source is the Object class, which is where the Object starts.

If a class does not specify a parent class, it inherits its parent class Object by default.

Let me give you an example to get the idea.

public class Student /*extends Object*/{
    //...
}
Copy the code

In all, it provides 11 methods in the Object class, such as equals(), hashCode(), toString(), getClass(), and so on.

And today, we’re not going to do that much, we’re going to do two things that are commonly used, that are often used in projects.

Class toString()

ToString () : The toString() method is called to convert an object to a string representation. If you haven’t overridden toString() in your class, it returns the object’s type +@+ memory address.

However, in real projects, you will often use toString to output specified content, so you will need to rewrite it. How do you do that? Check out my demo below:

@Data public class UserEntity{ private String name; private Integer age; private String sex; @Override public String toString() { return "UserEntity{" + "name=" + name + ",age=" + age + ",sex=" + sex + "}"; }}Copy the code

We can do a test wave on it, write a test class. Then new a User object, assign it a value, and see if it can be converted to our rewritten output by toString.

@Test public void test() { UserEntity user = new UserEntity(); User. Elegantly-named setName (" bug "); user.setAge(18); User. SetSex (" male "); String userStr = user.toString(); // Outputs toString. System.out.println(userStr); }Copy the code

Obviously. The console follows the output format we set for overriding toString(), and successfully gets the property values we set.

Equals ();

Now that the Object class is over, let’s talk about the equals() method it provides.

All it does is determine whether objects are “equal.”

If you call an object’s equals() method to compare another object, then the comparison case is discussed here as the score case.

1. Default address comparison

If you don’t override equals(), then you default to using the “==” operator to compare the memory addresses of two objects, returning false as long as they are not the same object, and true otherwise.

2. Object content comparison

If you want to compare the contents of objects, you have to override the logic provided by Object() to compare memory addresses by default and rewrite the execution logic yourself.

So how do I rewrite this? I can give you an example, just for reference:

@override public Boolean equals(Object obj) {return true if (this == obj) {return true; } // If the argument is empty or the type information is different, return false. if (obj == null || getClass() ! = obj.getClass()) { return false; } // convert to the current type UserEntity user = (UserEntity) obj; // Convert to type, and then compare the property values. return age == user.age && name == user.name && sex == user.sex; }Copy the code

The equals() override takes into account whether objects are empty, whether they are of the same type, etc., but the override logic is not unique. It depends on your actual business needs, so I’m just going to give you a little demo here, but don’t get me wrong.

As for the other methods provided by the Object class, such as hashCode() and getClass(), I will teach you later, ok? After all, in this episode, you’ll have to practice offline as well, since I didn’t give you an example of Equals, so I’d like you to make a supplement, and you’ll only find the problem in practice.

. .

Ok, the above is the basic use of class and object, if you have any questions about the article, welcome to comment on the comment area ah, I see will answer one by one.

Five, the end

Teaching is over, but some words I do not know but speak improper, ah ha ha, but I still want to say to you listen to. Here’s my favorite quote, and I’m going to send it to you in the hope that we can all become better and better.

Learning in no particular order, knowledge in no particular quantity; For every detail, ask for advice with an open mind; Three people, must have my teacher how!!

Talked to you!!

——————————————-

⭐️ if you like me, please pay attention to me.

⭐️ if it helps you, please click on it.

⭐️ If you have any questions, please leave a comment and let me know.

——————————————-