"Code Efficient-Alibaba Java Development Manual-Taishan Edition" Ali's latest version of the Taishan Java Development Manual, bid farewell to junk code

Preface

Dongyue Taishan; Xiyue Huashan; Nanyue Hengshan; Beiyue Hengshan; Zhongyue Songshan
Among them, Dongyue Taishan is the head, and the Taishan version of the Java development manual produced by Ali has gone through the collective wisdom and experience of the Alibaba technical team. The large-scale front-line actual inspection and continuous improvement, systematically organized into a book, are by far the most heavyweight.
Alibaba

What has been updated in the new version? It can be seen from the official version history.

Issue a unified solution for error codes

Alibaba

What are the error codes used for? The answer is the exception log, which is convenient for us to quickly know the source of the error and determine who caused the problem.

  • A in the above figure indicates that the error originated from the user;
  • There are also B-level, indicating that the error originated from the current system;
  • Level C indicates that the error originated from a third-party service, such as a CDN server.

This solution is still worth learning. Many mature systems are using error codes. If you have received WeChat payment, you should be familiar with error codes. Seeing the error code, and then searching in the manual, you can quickly know the type of error, which is still very good.

Click here to receive the manual for free, code csdn

34 new statutes added

There are too many 34, so I will pick a few important ones and talk about them.

1) Date and time

Remember the previous screen-scrolling events in the technology circle? It is the problem caused by YYYY and yyyy. The uppercase Y indicates which year the current week belongs to, and the lowercase y indicates the year of the current day. The difference is still quite big. You taste, you taste carefully.

Also, uppercase M and lowercase m are different, and uppercase H and lowercase h are also different.

Alibaba

In addition, you should use System.currentTimeMillis() instead of new Date().getTime() to get the current milliseconds . These detailed specifications should be kept in mind and don't make these low-level mistakes.

2) NPE problem of trinocular operation

To be honest, I have never paid attention to this problem before. I saw it this time, so let's learn it together. First look at the following code:

public class TestCondition {
    public static void main(String[] args) {
        Integer a = 1;
        Integer b = 2;
        Integer c = null;
        Boolean flag = false;
        Integer result = flag ? a * b : c;
    }
}

The condition a * b is an arithmetic operation, and the result of their multiplication is an int type, which will cause the Integer type c to be automatically unboxed. Because the value is null, the following error is thrown:

Exception in thread "main" java.lang.NullPointerException
    at com.cmower.mkyong.TestCondition.main(TestCondition.java:9)

You may be curious about why two variables of Integer type are multiplied into an int type. This is mainly determined by the compiler. It is designed like this. Let’s take a look at the decompiled bytecode:

public class TestCondition
{
    public static void main(String args[])
    {
        Integer a = Integer.valueOf(1);
        Integer b = Integer.valueOf(2);
        Integer c = null;
        Boolean flag = Boolean.valueOf(false);
        Integer result = Integer.valueOf(flag.booleanValue() ? a.intValue() * b.intValue() : c.intValue());
    }
}

When a * b is automatically unboxed, the intValue() method is called, and the types of the two expressions of the ternary operation must be the same, which causes c to also call the intValue() method. Since c itself is null, then It can only be NPE . you got it?

3) The toMap() method of the Collectors class

The manual says that when using the toMap() method of the java.util.stream.Collectors class to transfer to a Map, you must use a method with a parameter type of BinaryOperator and a parameter name of mergeFunction , otherwise an IllegalStateException will be thrown when the same key value appears abnormal.
This passage may be a bit difficult to understand, so let's look at a piece of code first!

String[] departments = new String[] {"芸芸", "芸芸", "芸芸";
Map<Integer, String> map = Arrays.stream(departments)
        .collect(Collectors.toMap(String::hashCode, str -> str));

When running this code, an exception will be thrown, and the stack information is as follows:

Exception in thread "main" java.lang.IllegalStateException: Duplicate key 867758096 (attempted merging values 芸芸and 芸芸)
    at java.base/java.util.stream.Collectors.duplicateKeyException(Collectors.java:133)
    at java.base/java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$1(Collectors.java:180)

The key is duplicated, because the hashCode of the two "Yunyun" is the same. What's the solution at this time?

String[] departments = new String[] {"芸芸", "芸芸", "芸芸"};
Map<Integer, String> map = Arrays.stream(departments)
        .collect(Collectors.toMap(String::hashCode, str -> str, (v1, v2) -> v2));

Add one more parameter (v1, v2) -> v2 , that is, choose one when repeating. Let 's take a look at the toMap() method called at this time .

public static <T, K, U>
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
                                Function<? super T, ? extends U> valueMapper,
                                BinaryOperator<U> mergeFunction) {
    return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}

Did BinaryOperator and mergeFunction appear?

Click here to receive the manual for free, code csdn

Modify the description 90 places

According to the manual, for example, the descriptions of blocking waiting for locks and the decimal type of the table have been modified. It took me half an hour to find out the difference with the previous version.
Alibaba

I don't know if the editor of the manual is talking nonsense, if you can find the difference, let me know.

Click here to receive the manual for free, code csdn

Improve some examples

For example, the ISNULL example in the SQL statement column does add a more detailed counterexample than the Huashan version, as shown in the figure below.

Insert picture description here
But to be honest, I read this counterexample description at least six times before I understood what it meant. First of all, do not wrap before null, which will affect the readability. This is true; secondly, do not use column is null to determine empty, use ISNULL(column) to determine empty , which is more efficient and will not cause line breaks.

select * from cms_subject where column is null and
column1 is not null;

select * from cms_subject where ISNULL(column) and
column1 is not null;

At last

In December 2016, Ali released this "Java Development Manual" to the industry for the first time. It has been more than 3 years since the Taishan version was released. With the joint efforts of Java developers around the world, this manual has become common in the industry. Follow the development specifications. The knowledge points contained in this manual are very comprehensive. The seven dimensions of programming specifications, exception logs, unit testing, security specifications, MySQL database, engineering specifications, and design specifications are all listed.

If you want to be a good Java engineer, then the content of this manual is almost a must. If you don’t have this manual yet, you can click here to get it for free, with the code csdn . If you already have a programmer, please ignore it. Finally, I sincerely wish you, I hope you can learn something, come on!

Alibaba
Alibaba

Those who need this document can click here to get it for free . For programmers who already have the code csdn , please ignore it.

Guess you like

Origin blog.csdn.net/yueyunyin/article/details/109199058