5, Object general method

1. The equals ()

The five characteristics

/ / reflexivity
x.equals(x); // true

/ / symmetry
x.equals(y) == y.equals(x); // true

/ / deliverables
if (x.equals(y) && y.equals(z))
    x.equals(z); // true;
    
/ / consistency
x.equals(y) == x.equals(y); // true

X calls x.equals(null) on any object that is not null, and the result is false
x.equals(null); // false;
Copy the code

= = and equals

For basic types, == determines whether two values are equal. There is no equals() method for basic types.

For reference types, == determines whether two variables refer to the same object, and equals() determines whether the objects referenced are equivalent.

Integer x = new Integer(1);
Integer y = new Integer(1);
System.out.println(x.equals(y)); // true
System.out.println(x == y);      // false
Copy the code

implementation

Check if it is a reference to the same object, and return true if it is;

Check if it is the same type. If not, return false.

Transform Object;

Determine whether each key domain is equal.

2, hashCode ()

Always override the hashCode() method when overriding the equals() method, ensuring that two objects with equivalent hash values are also equal.

Collection classes such as HashSet and HashMap use the hashCode() method to calculate where objects should be stored, so to add objects to these collection classes, you need to have the corresponding class implement the hashCode() method.

An ideal hash function should be homogeneous, that is, unequal objects should be evenly distributed across all possible hash values. And that requires the hash function to take into account all the values of the fields. You can treat each field as a bit in base R and form an integer in base R.

R is usually 31, because it’s an odd prime, and if it’s even, when you have a multiplication overflow, you lose the information, because when you multiply by 2 you just move one bit to the left, you lose the leftmost bit. And a number multiplied by 31 converts to shift and subtraction: 31*x == (x<<5)-x, and the compiler does this optimization automatically.

@Override
public int hashCode(a) {
    int result = 17;
    result = 31 * result + x;
    result = 31 * result + y;
    result = 31 * result + z;
    return result;
}
Copy the code

3, toSting ()

By default, the form ToStringExample@4554617c is returned, where @ is followed by an unsigned hexadecimal representation of the hash code.

4, clone ()

cloneable

Clone () is a protected method on an Object. It is not public. If a class does not explicitly override Clone (), other classes cannot directly call the clone() method on an instance of that class.

It should be noted that the Clone () method is not a method of the Cloneable interface, but is a protected method of Object. Cloneable interface just rules, if a class does not implement the Cloneable interface again call clone () method, which will throw CloneNotSupportedException.

Shallow copy

The reference type of the copied object and the original object refer to the same object. The copy depth is not high. Modifying the property value of the original object will also change the property value of the copied object

Deep copy

The reference types of the copied object and the original object refer to different objects. The copied object and the original object are independent of each other.

An alternative to Clone ()

Copying an object using the Clone () method is complex and risky, it throws exceptions, and it requires type conversions. Instead of using Clone (), use the copy constructor or copy factory to copy an object, according to the Effective Java book.

Six, inheritance,

Access permissions

There are three access modifiers in Java: private, protected, and public. Without the access modifier, the package level is visible.

Abstract classes and interfaces

1. An abstract class

Abstract classes and methods are declared using the abstract keyword. If a class contains abstract methods, then the class must be declared abstract.

The main difference between an abstract class and a normal class is that an abstract class cannot be instantiated, only inherited.

2. The interface

An interface is an extension of an abstract class. Prior to Java 8, it could be considered a completely abstract class, that is, it could not have any method implementations.

Since Java 8, interfaces can also have default method implementations because interfaces that do not support default methods are too expensive to maintain. Before Java 8, if an interface wanted to add a new method, all classes that implemented the interface would be modified to implement the new method.

Interface members (fields + methods) are public by default and are not allowed to be private or protected. Starting with Java 9, it is possible to define methods as private, so you can define some reusable code without exposing the methods.

The fields of an interface are static and final by default.

3. Compare

At the design level, abstract classes provide an IS-A relationship that satisfies the substitution principle in which A subclass object must be able to replace all of its parent objects. Interfaces, on the other hand, are more LIKE A like-a relationship, which provides A way to implement the contract and does not require an IS-A relationship between the interface and the class implementing the interface.

In use, a class can implement more than one interface, but it cannot inherit from more than one abstract class.

Fields of an interface can only be static and final. Fields of an abstract class have no such restriction.

Members of an interface can only be public, whereas members of an abstract class can have multiple access rights.

4. Select

Using interfaces:

Unrelated classes need to implement a method, such as the compareTo() method in the Comparable interface;

Multiple inheritance is required.

Using abstract classes:

You need to share code among several related classes.

You need to be able to control access to inherited members, rather than making them all public.

Non-static and non-scalar fields need to be inherited.

In many cases, interfaces take precedence over abstract classes. Because interfaces don’t have the strict class hierarchy requirements of abstract classes, you have the flexibility to add behavior to a class. And since Java 8, interfaces can also have default method implementations, making the cost of modifying the interface very low.

super

Access to the constructor of the parent class: You can use the super() function to access the constructor of the parent class, thus delegating some initialization to the parent class. It should be noted that a subclass must call the superclass’s constructor to do the initialization. This is usually the default constructor of the superclass. If a subclass needs to call any other superclass constructor, it can use the super() function.

Access to members of the parent class: If a subclass overrides a method of the parent class, it can be done by referring to the method of the parent class using the super keyword.

Overwriting and overloading

1. Override

In inheritance, when a child class implements a method that is identical in method declaration to the parent class.

In order to satisfy the substitution principle, the rewrite has the following three limitations:

The access permission of a subclass method must be greater than or equal to that of a superclass method.

The return type of a subclass method must be either the return type of the superclass method or a subclass.

The type of exception thrown by a subclass method must be either the type of the superclass or a subclass.

When a method is called, it first looks in the class to see if there is a corresponding method. If there is no method, it then looks in the superclass to see if it inherited from the superclass. Otherwise, you have to transform the parameter, turn it into a parent class and see if there’s a corresponding method. In general, the priority of a method call is:

this.func(this)

super.func(this)

this.func(super)

super.func(super)

(2) Overload

In the same class, a method that has the same name as an existing method but has at least one different argument type, number, or order.

It should be noted that if the return value is different and everything else is the same, it is not an overload.