JAVA commonly used API - Object and Objects

Article directory

Table of contents

Article directory

foreword

1. What is Object?

2. Common methods of Object

1.tostring

1.1 return value

1.2 Rewrite the toString method

3. clone (clone)

1. Clone interface

 三.Objects

Summarize



foreword

Hello everyone, I love animal milk the most. Today I will tell you about objects in java and the tool objects of objects.

So, follow me to have a look!


 

1. What is Object?

Object is the top-level parent class in java, all classes will directly or indirectly inherit Object


2. Common methods of Object

1.tostring

1.1 return value

 Above we see that the toString method in the parent class Object returns a string of unknown strings, so when we use the toString method, we need to rewrite the toString method

1.2 Rewrite the toString method


 2.equals

For detailed usage and analysis of equals, please refer to http://t.csdn.cn/QauYZ


3. clone (clone)

1. Clone interface

In java, if you want to clone an object, you must implement the clone interface Cloneable

 

 

The above cloning text explanation is a bit wrong, add:

Shallow copy means that when copying an object, only its reference is copied, not the object itself. That is to say, the copied object and the original object share the same reference, and they point to the same object. In this case, if the attribute value of the copied object is modified, the attribute value of the original object will be affected.

Deep copy refers to copying an object, not only copying its reference, but also copying the object itself. In other words, the copied object and the original object are two independent objects, and their attribute values ​​do not affect each other.

 

Verify that the clone method of the parent class is a shallow copy

At this time, someone may ask

Isn't String a reference data type? Shouldn't the reference data type be a deep copy?

Why is it also a shallow copy? In fact, this is related to the immutability of String

 


 三.Objects

As the name suggests, the tool class of object

Object is an object tool class that provides some methods

public static boolean equals(Object a, Object b)

public static boolean isNull(Object obj)

Determine whether the object is null, return true for null1, otherwise return false

public static boolean nonNull(Object obj)

Determine whether the object is null, opposite to the result of isNull

equals will not be introduced here

 

 

Summarize

The above is the main content of this blog post. You should focus on understanding toString, equals, clone

 

Guess you like

Origin blog.csdn.net/weixin_73869209/article/details/130670490