Java developers most commonly committed 10 errors

Original link: https://www.cnblogs.com/chenpi/p/5508949.html

This list summarizes the error 10 Java developers most commonly committed.


640?wx_fmt=png


When you need to turn into Array ArrayList when developers often do:

640?wx_fmt=png

Arrays.asList () returns an ArrayList, but pay special attention to, this is a static inner class Arrays ArrayList class, not java.util.ArrayList class. java.util.Arrays.ArrayList class implements the set (), get (), contains () method, but does not implement ways to increase the element (in fact, you can call the add method, but no specific implementation, just throw an UnsupportedOperationException ), so its size is fixed. In order to create a real java.util.ArrayList, you should do:

640?wx_fmt=png


ArrayList constructor may receive one type Collection, and has been achieved java.util.Arrays.ArrayList interface.

Developers may often do so:

640?wx_fmt=png


The above code will work, but there is no need to convert it into a set collection, will turn into a List Set requires additional time, in fact, we can simply use the following methods to:


640?wx_fmt=png

or

640?wx_fmt=png

The first method more readable.

Consider the following code, delete elements during the iteration:

640?wx_fmt=png

Print Results:
[B, D]

There are a series of problems in the above method, when an element is removed, list size is reduced, then the original index points to other elements. So if you want to delete more elements by index in the loop will not work correctly.

You may know is iterators delete elements in the loop right way, perhaps you know foreach loop with iterators is very similar, but the fact is not the case, the following code:

640?wx_fmt=png

ConcurrentModificationException will throw an exception.

But then the code is OK:

640?wx_fmt=png


next () method needs to be called before the remove () method, the foreach loop, the compiler will call the next delete elements of the operating method, which leads to ConcurrentModificationException exception. For more detailed information, you can check ArrayList.iterator () source code.


From an algorithmic point of view, HashTable name is a data structure. In Java, however, such a data structure called a HashMap. One major difference is HashTable and HashMap HashTable is synchronized, so, generally speaking, you would use HashMap, rather than Hashtable.

In Java, primitive types (raw type) and unbounded wildcard type is very confusing. To give an example of Set, Set is the original type, and Set <?> Is unbounded wildcard type.

Consider the following code, add method uses a primitive type List as entry parameters:

640?wx_fmt=png

Run the above code will throw an exception:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at ...

Use the original type of the collection is very dangerous, because it skips the generic type checking, it is unsafe. In addition, Set, Set <?>, And Set <Object> These three are very different.

Developers often use public-modified class field, although it is very easy for others to get the value of this field directly by reference, but this is a bad design. According to experience, you should reduce the access level member properties as possible.

Why do developers often use ArrayList and LinkedList, do not know the difference between them, because they look like. However, it has a huge performance difference between them. Simply put, if a large number of deletions and no increase in random access operation of many elements, should be preferred LinkedList.

Immutable object has many advantages, such as simple, safety and the like. But a different value for each requires a separate object, too many objects can cause a lot of garbage, so the choice of variable and immutable, the need to have a balance.

Typically, variable object to avoid a large number of intermediate object, a classic example is the large number of splicing string. If you use an immutable object, it will immediately generate a large number of objects eligible for garbage collection standards, which wastes a lot of CPU time and effort. Using variable objects is the right solution (StringBuilder);

640?wx_fmt=png


In addition, in some other cases also require the use of variable objects. For example, a method to pass a variable object, and then collect a variety of results, without the need to write many grammar. Another example is sorting and filtering: Of course, you can write a method to receive the original set and returns a sorted collection, but as for large collections too wasted.


640This compilation error has arisen because the default constructor of the parent class method is not defined. In Java, if a class does not define a constructor method, the compiler will insert a default constructor with no arguments; but if a constructor defined in the parent class, in this case, the compiler is not automatically insert a default the constructor with no arguments, which is the case more than a demo;

For the sub-category, whether it is still a no-argument constructor argument constructor method, a default constructor with no arguments will call the parent class; when the compiler tries to insert these two super constructor in the subclass () method, because the parent class does not have a default constructor with no arguments, the compiler error;

To fix this error, it is simple:

1, a parent class to manually define a constructor with no arguments:

640?wx_fmt=png


2, removal of the parent class constructor custom

3, to write their own subclasses on the call parent class constructor; as super (value);

There are two ways to create a string:

640?wx_fmt=png


What is the difference between them?

The following code provides a quick answer:

640?wx_fmt=png

This list is based on open source projects I'm on a lot of github, questions on Stack overflow, as well as analysis of some of the popular google search. Assessment shows no clear proof that they are the top 10, but they definitely are very common. If you disagree with any part, please leave your comment. If you can make some other common mistakes, I will be very grateful.

Guess you like

Origin blog.csdn.net/xmt1139057136/article/details/100571448