preface

We often encounter some inattention pit in the process of use, today simply list some common problems

NPE appears in unpacking

As shown in the figure below, the basic type is used in the method. When calling, no NULL is detected and the packaging type is passed in directly, which is very easy to cause the OCCURRENCE of NPE in unpacking

Suggestion: Use a wrapper type for method entries

A phenomenon caused by an odd integer cache

In Java, integers such as Integer, Short, and Long exist in cache. By default, the same object is returned between -127 and 128. Therefore, you can set the parameter -xx :AutoBoxCacheMax=128 to change the maximum cache size.

Suggestion: Compare data using equals for wrapped types, or use == for all primitive types

Arrays.asList is prone to exceptions due to improper use

AbstractList Because arrays. asList returns a list that belongs to AbstractList without add or remove methods, exceptions will be thrown once it is used

Suggestion: You can pass the result of arrays.asList to the ArrayList constructor as follows

List ints = new ArrayList<>(Arrays.asList(1, 2, 3));

Operates on arguments returned by methods that return collections such as List

Because in daily development, many teams require that there is no data to return the collection method, but also need to return an empty collection, as follows

Once return empty collections for use, direct use, may appear UnsupportedOperationException.

Suggestion: You are not advised to add or remove the returned data directly. You are advised to add or remove the returned data through a user-defined set, as follows

List list = getList();

List arrayList = new ArrayList<>();

arrayList.addAll(list);

arrayList.add(“1”);

List. ToArray accidentally returns an error

The toArray method is usually used to convert a list set to an array, but because of generic erasure, the actual storage is an Object array, so it cannot be strongly converted, as shown below

Suggestion: Use toArray to specify the return type

List list = new ArrayList<>(Arrays.asList(“a”,”b”,”c”));

String[] strings = list.toArray(new String[0]);

The collection is removed in the for loop

Experienced developers are familiar with the problem of removing elements in the for loop, which causes the collection to fail quickly and throw an exception

Suggestion: Use Iterator to remove

Method overload, resulting in method conflicts

As follows, method overloading works normally due to normal type matching, and once null is passed in, the JVM has no way of knowing which method is actually being called.

In fact, compile time is not passable, and you need to specify which type null is.

Using the split method tends to omit the last name

Split method needs to pay attention to two points, one is that the parameters can be filled in regular expression, so some special symbols need to escape; Another is that when a continuous delimiter appears at the end, the content in the middle is ignored, as follows

When using BigDecimal, there are also precision issues that can arise without notice

When using BigDecimal, do not use constructors such as double to build objects, as shown below

Suggestion: Always use the String constructor for BigDecimal

Use Executors to create threads, when threads are handled too slowly, easy OOM

For the thread pool created by Executors, the maximum number of threads is either Integer.MAX_VALUE or the unbounded queue is used. In high concurrency scenarios, OOM is most likely to occur

Suggestion: Create a thread pool by specifying parameters using ThreadPoolExecutor

Types such as weak references are not destroyed after gc if they are used incorrectly

There are strong references, soft references, weak references, and virtual references in Java. Soft references are released when the JVM runs out of memory. Weak references are collected when gc is performed.

For example, weak references that are not used properly will not be recycled when gc occurs, and soft references that are not used properly will not be recycled even after OOM.

The reason for this is that STR is a strong reference, so weak applications are not recycled when gc is performed.

The correct way to use it is as follows

If there are other scenes of the pit, welcome to leave a message, thank you!