2. [Mandatory] All Override methods must have the @override annotation.
Description: getObject() and get0bject() problems. One is the letter O, the other is the number 0, plus @override can accurately determine whether the overwrite is successful, to avoid overwrite failure caused by too similar appearance. In addition, if you change a method signature in an abstract class, the implementation class will compile an error immediately.
Overrides occur at run time, when subclasses rewrite the implementation of the parent class’s accessible methods. The return value type, method name, and parameter list must be the same. The range of exceptions thrown must be smaller than or equal to the parent class. The range of access modifiers must be larger than or equal to the parent class.
If the parent method access modifier is private/final/static, the subclass cannot override the method, but static methods can be declared again.
The constructor cannot be overridden.
3. [Mandatory] Java variable parameters can be used only when the parameter types and service meanings are the same, instead of Object.
Is:
public List<User> listUsers(String type, Long... ids) {... }Copy the code
Note: Variable arguments must be placed at the end of the argument list. (Students are encouraged not to use variable parameter programming.)
Java mutable arguments are a new feature in version 1.5. This means that users who want to define a method but don’t know how many parameters they want to pass in the method can write the parameter type or array name in the parameter list of the method and then operate directly inside the method as an array. This applies to the situation where the number of parameters is uncertain and the type is determined.
Can refer to the article: blog.csdn.net/w605283073/…
4. [Mandatory] The method signature is not allowed to be modified for the interface that is being called externally or the interface that the library depends on, so as to avoid impact on the interface caller. Obsolete interfaces must be annotated with @deprecated and clearly state what the new interface or service is being adopted.
Description: Method signatures are method names + parameter types, such as the following method
public double calculateAnswer(double wingSpan, int numberOfEngines,
double length, double grossTons) {
//do the calculation here
}
Copy the code
The method signature is calculateAnswer(double, int, double, double) and the code at the caller is bound to report an error if it is modified.
Deprecated annotation. When a class or method is Deprecated, the Deprecated annotation indicates that the method or method is no longer recommended. The Deprecated annotation also indicates that the method or method is Deprecated, but does not mean that it is Deprecated because there are better methods available. The original method is not deleted directly because it may need to be called in many places. The deletion is too much work, so we can only use this annotation to explain the obsoletion, and let future developers use the new method.
6.【 mandatory 】Object’s equals method is prone to nullpointer exceptions. Call equals using constants or objects that are determined to have values.
Is: “test”. The equals (object); Example: the object. The equals (” test “); Note: It is recommended to use java.util.objects #equals (the utility class introduced in JDK7) internally
// 1. Objects.equals(Object a, Object b)
public static boolean equals(Object a, Object b) {
return(a == b) || (a ! = null && a.equals(b)); } // 2. Object.equals(Object obj) public boolean equals(Object obj) {return (this == obj);
}
Copy the code
If the Object type does not override equals, it calls the equals method of the Object (which is still the reference comparison of the Object). Null is also considered.
The utility class also has hashCode() and toString() methods, which are easy to use
8. Criteria for the use of basic data types and wrapper data types are as follows:
1) [Mandatory] All POJO class attributes must use wrapper data types.
The return values and parameters of RPC methods must use wrapper data types.
3) [Recommendation] Use basic data types for all local variables.
Example: The query result of the database may be null because of automatic unpacking and NPE risk of receiving with basic data type.
Counterexample: for example, the rise and fall of total transaction volume is displayed, that is, plus or minus X %. X is the basic data type, and the RPC service is called. When the call is unsuccessful, the default value is returned and the page displays 0%, which is unreasonable and should be displayed as a dash. So the null value of the wrapper data type can indicate additional information, such as: remote call failure, abnormal exit.
Note: The absence of initial values for POJO class attributes reminds users that they must explicitly assign values when needed. Any NPE (NullPointException) issues, or entry checks, are guaranteed by the user. We need to take note of this for ourselves. Wrapper datatypes are rarely used in normal development and are mostly basic datatypes that need to be corrected.
10. [Mandatory] Do not modify the serialVersionUID field when serializing new attributes of the class to avoid anti-sequence failure; Change the serialVersionUID value if the upgrade is completely incompatible to avoid deserialization clutter.
Note: Note that inconsistent serialVersionUID throws a serialization runtime exception.
SerialVersionUID applies to Java’s serialization mechanism. Simply put, Java’s serialization mechanism verifies version consistency by determining the serialVersionUID of a class. During deserialization, the JVM will compare the serialVersionUID in the byte stream sent from the JVM with the serialVersionUID of the corresponding local entity class. If the serialVersionUID is the same, it is considered the same and can be deserialized. Otherwise, the serialVersionUID will be inconsistent. Is InvalidCastException.
SerialVersionUID has two ways to generate a display:
The first is the default 1L, for example:
private static final long serialVersionUID = 1L;
Copy the code
The second is a 64-bit hash field calculated based on the package name, class name, inheritance relationship, non-private methods and attributes, as well as parameters, return values, and many other factors. Basically, this is the only value that you can compute. Such as:
private static final long serialVersionUID = xxxxL;
Copy the code
In this case, if the Class file (Class name, method name, etc.) does not change (adding whitespace, newlines, comments, etc.), the serialVersionUID will not change even if it is compiled multiple times, which is incompatible.
18. The string concatenation method in the loop is extended using the Append method of StringBuilder.
Example:
String str = "start";
for (int i = 0; i < 100; i++) {
str = str + "hello";
}
Copy the code
Note: In this example, the decomcompiled bytecode file shows that each time the loop returns a StringBuilder object, the append operation is performed, and the toString method returns a String object, resulting in a waste of memory resources.
Strings are immutable because they use the final keyword to modify character arrays to hold strings. Both StringBuilder and StringBuffer inherit from AbstractStringBuilder classes, AbstractStringBuilder uses an array of characters to hold the string char[]value, but not the final keyword, so both objects are mutable. The JVM optimizes the string connection to the AppEnd of A StringBuilder, but consumes resources, generating a StringBuilder each time in the loop.
Visible: www.cnblogs.com/rjhlovelife…
20. [Recommendation] Carefully copy objects using the Clone method of Object. Note: The clone method of objects is shallow copy by default. If you want to achieve deep copy, you need to rewrite the Clone method to achieve deep traversal copy of domain objects.
Note: For reference types, shallow copy only copies Pointers to an object, not the object itself. The old and new objects still share the same block of memory. Modifying the new object will cause the original object to change. But deep copy will create another identical object, the new object and the original object do not share memory, modify the new object will not change to the original object. Deep copy can be implemented by serialization and recursive rewriting of the Clone method.
The last
Welcome to pay attention to the public number [I do not know goodbye to ask me], thank you ~