8 technical mistakes JAVA programmers are prone to make

It is said that the Java language is a simple programming language, evolved from C++ , and eliminates many complex features in C++ , but this does not guarantee that Java programmers will not make mistakes. So for the majority of Java programmers, what are the 10 most common mistakes they make?

1. Two construction methods of string objects

String objects in Java are created in two common ways:

//1. use double quotesStringx="abc";//2. use constructorStringy=newString("abc");

  What is the difference between them? Let's look at the following code again:

Stringa="abcd";Stringb="abcd";System.out.println(a==b);// TrueSystem.out.println(a.equals(b));// TrueStringc=newString("abcd");Stringd=newString("abcd");System.out.println(c==d);// FalseSystem.out.println(c.equals(d));// True

2. Mutable vs Immutable

  Immutable objects have many advantages, such as simplicity, safety, etc. However, for each distinct value, an object of that class is required. Also, the problem with generating many objects is that they can lead to frequent garbage collections. Therefore, when choosing a mutable class or an immutable class, you should make a decision after comprehensive consideration.

  Generally speaking, mutable objects avoid creating a large number of intermediate objects. A very classic example is chaining a large number of short String objects into one long String object. If the immutable String class is used, the chaining process will generate a large number of intermediate String objects suitable for immediate garbage collection , which will consume a lot of CPU performance and memory space. At this point, it is only correct to use a mutable StringBuilder or StringBuffer .

Stringresult="";for(Strings:arr){result=result+s;}

  In addition to the above cases, mutable objects may be due to immutable objects in other scenarios. For example, passing a mutable object inside a method can be used to collect multiple results without jumping in and out of multiple loop levels.

3ArrayList vs LinkedList

  Many Java beginners don't understand the difference between ArrayList and LinkedList , so they only use the relatively simple ArrayList , and they don't even know that LinkedList exists in the JDK . However, in some specific scenarios, the choice of these two Lists will lead to huge differences in program performance. In short: when there are many add/remove operations in the application scenario and only a few random access operations, LinkedList should be selected; in other scenarios, consider using ArrayList .

4

access permission

  Many Java beginners like to use public to decorate members of a class. This makes it easy to directly access and access the member. However, this is a very bad programming style, and the correct design style should be to reduce the access rights of class members as much as possible.

5. Use primitive types in Collections

  In Java , it's easy to confuse primitive types with infinite wildcard types. Let's take an example related to Set : Set is a primitive type; Set is an infinite wildcard type. Let's see an example of using primitive types in List :

publicstaticvoidadd(Listlist,Objecto){list.add(o);}publicstaticvoidmain(String[]args){Listlist=newArrayList();add(list,10);Strings=list.get(0);}

  This sample code will throw an exception: Exceptioninthread"main"java.lang.ClassCastException:java.lang.Integercannotbecasttojava.lang.Stringat...

  Using primitive types in Collections carries a lot of risk of type errors because primitive types have no static type checking. Actually, there is a very big difference between Set , Set and Set .

6Hashtable vs HashMap

  Readers who have studied data structures know that a very important data structure is called a hash table. In Java , the class corresponding to the hash table is HashMap instead of Hashtable . The core difference between HashMap and Hashtable is: HashMap is asynchronous, Hashtable is synchronous.

7. Whether the array contains a specific value

  To check if an array contains a certain value, many Java programmers use code like this:

Setset=newHashSet(Arrays.asList(arr));returnset.contains(targetValue);

  Functionally, the code is correct, but it consumes a lot of performance in the process of Array to List, List to Set . We can optimize it into the following form:

Arrays.asList(arr).contains(targetValue);

  Or, further optimized into the most efficient code as follows:

for(Strings:arr){if(s.equals(targetValue))returntrue;}returnfalse;

8. Array to ArrayList

  To convert an array into an ArrayList , many Java programmers use the following code:

Listlist = Arrays.asList (arr);

Arrays.asList does return an ArrayList object, but that class is a private static inner class within the Arrays class, not the common java.util.ArrayList class. This java.util.Arrays.ArrayList class has methods like set() , get() , contains() , but it doesn't have any method to add or remove elements. Because the size of the class (size) is fixed. To create a real java.util.ArrayList, the code should look like this:

ArrayListarrayList=newArrayList(Arrays.asList(arr));

  We know that the constructor of ArrayList can accept an object of type Collection , and our java.util.Arrays.ArrayList happens to be a subclass of it. In fact, a more efficient code example is:

ArrayListarrayList=newArrayList(arr.length);Collections.addAll(arrayList,arr);

The above is by summarizing the 8 most common mistakes Java programmers make, which can effectively help Java latecomers avoid detours, work less overtime, and write more robust applications.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325646784&siteId=291194637