It has been 10 months since the last release of Java Development Manual of Ali, and this release also added a lot of dry contents, such as: 34 new specifications, 90 modified descriptions, including error code rules for the first time to put forward a complete solution, the release log is as shown in the figure below:

Added content Overview

The main updated contents of this update include:

  1. Added error code specification;
  2. Extended date (leap year, leap month, etc.) processing scheme;
  3. Table alias specification;
  4. Ternary operator specification, etc.

Let’s take a look at what these additions mean.

1. Error code specifications

There was no overall description of “error code” in the previous version, but this version has increased a lot of space to make a detailed specification and description of the “error code” part, which is an important link of the front and back end coordination. The specific specifications are as follows:

  • [Mandatory] Error code formulation principle: fast traceability, easy to remember, communication standardization.
  • [Mandatory] The error code does not contain the version number or error level information.
  • [Mandatory] Returns five zeros: 00000 if all are normal, but an error code must be filled.
  • [Mandatory] The error code is a string of five characters, which are divided into two parts: error source + four-digit number.

Note: Error sources are A/B/C:

  • A: Indicates that the error is caused by the user, for example, the parameter is incorrect, the version installed by the user is too early, or the payment times out.
  • B: Indicates that the error originates from the current system, which is usually caused by a service logic error or poor program robustness.
  • C: Indicates that the error originates from the third-party service, such as CDN service error or message delivery timeout. The four-digit number ranges from 0001 to 9999. The step spacing between categories is reserved for 100. For details, see the Java Development Manual.

2. Extended the date processing scheme

A number of days in a year written to 365 days gives everyone a lot of pain in leap years, for example, cache expiration = 3652460*60 seconds, which results in all caches failing at the same time a day earlier, and the database falling down on high concurrency.

[Mandatory] Do not write a year as 365 days in the program, to avoid date conversion errors or program logic errors in the Gregorian calendar leap year.

Is:

// Get the number of days this year
int daysOfThisYear = LocalDate.now().lengthOfYear();
// Gets the number of days in a specified year
LocalDate.of(2011.1.1).lengthOfYear();
Copy the code

Example:

// The first case: an array out-of-bounds exception occurs on 366 days in a leap year
int[] dayArray = new int[365];
// The second case: one-year membership, registered on January 26th of this year, hard-coded 365 returns on January 25th
Calendar calendar = Calendar.getInstance();
calendar.set(2020.1.26);
calendar.add(Calendar.DATE, 365);
Copy the code

3. Table alias specification

[Mandatory] For the query and change of the table record in the database, as long as multiple tables are involved, the table name must be qualified. Note: When querying, updating, or deleting records from multiple tables, an exception will be thrown if the alias (or table name) of the operation column is not specified and the operation column exists in multiple tables. Select t1.name from table_first as t1, table_second as t2 where t1.id=t2.id;

Example: In a service, a field with the same name is added to a table because the associated query statement of multiple tables is not limited by the alias (or table name) of the table. After two years of normal operation, a 1052 exception occurs in the online query statement after the database is changed in the pre-release environment: Column ‘name’ in field list is ambiguous.

[Recommended] Add as before the alias of a table in an SQL statement and enter the name T1, T2, t3,… The order is named in turn. Description:

  • An alias can be a short name of a table or named t1, T2, or T3 according to the order in which the table appears.
  • An alias is preceded by as to make it easier to identify. Select t1.name from table_first as t1, table_second as t2 where t1.id=t2.id;

4. Specification of ternary operators

Originally in the ternary operator:

condition ? Expression 1: expression 2

Expressions 1 and 2 trigger automatic unboxing when arithmetic calculations or data type conversions are involved. The unpacking operation was fine, but when the operand was null, a large NPE hit me in the face, as shown in the following code:

Integer a = 1;
Integer b = 2;
Integer c = null;
Boolean flag = false;
Integer result = (flag ?  a * b  :  c);
Copy the code

Note: if the result of a* B is int, c will unpack the container to int and raise an NPE exception.

Condition? Expression 1: In expression 2, it is highly noted that expressions 1 and 2 May throw an NPE exception caused by automatic unpacking when their types are aligned.

Note: Type aligned unpacking operations are triggered in the following two scenarios:

  • Either expression 1 or expression 2 is of primitive type.
  • Inconsistent types of values in expression 1 or expression 2 force unboxing to upgrade to the type representing the wider range.

conclusion

Ali’s Java Development Manual was required reading for every new employee in our company and played a huge role in my Java programming career. Literally, the influence of its birth and assist the Java industry as a whole, this is just the attached new version of the Java development manual (mount tai), part of the content, more wonderful content, click on the download: pan.baidu.com/s/1tyUFbfOG… Password: o62a.

Thanks & thanks

Java Development Manual (Taishan Edition)

“The Strongest Java Development Manual in history”

For more exciting content, please follow the wechat public account “Java Chinese Community”.