This list summarizes the top 10 mistakes that Java developers frequently make.
#1. Convert Array to ArrayList To convert an array to an
The constructor of #2. Check If an Array Contains a Value Developers often do:
The code works, but there is no need to convert a list to set first. Converting a list to a set requires extra time. It can as simple as:
or
The first one is more readable than the second one. #3. Remove an Element from a List Inside a Loop Consider the following code which removes elements during iteration:
The output is: [b, d] There is a serious problem in this method. When an element is removed, the size of the list shrinks and the index changes. So if you want to delete multiple elements inside a loop by using the index, that will not work properly. You may know that using iterator is the right way to delete elements inside loops, and you know foreach loop in Java works like an iterator, but actually it is not. Consider the following code:
It will throw out ConcurrentModificationException. Instead the following is OK:
#4. Hashtable vs HashMap By conventions in algorithm, Hashtable is the name of the data structure. But in Java, the data structure's name is HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap #5. Use Raw Type of Collection In Java, raw type and unbounded wildcard type are easy to mixed together. Take Set for example, Consider the following code which uses a raw type
This code will throw an exception: Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at ... Using raw type collection is dangerous as the raw type collections skip the generic type checking and not safe. There are huge differences between #6. Access Level Very often developers use public for class field. It is easy to get the field value by directly referencing, but this is a very bad design. The rule of thumb is giving access level for members as low as possible. public, default, protected, and private #7. ArrayList vs. LinkedList When developers do not know the difference between #8. Mutable vs. Immutable Immutable objects have many advantages such simplicity, safety, etc. But it requires a separate object for each distinct value, and too many objects might cause high cost of garbage collection. There should be a balance when choosing between mutable and immutable. In general, mutable objects are used to avoid producing too many intermediate objects. One classic example is concatenating a large number of strings. If you use an immutable string, you would produce a lot of objects that are eligible for garbage collection immediately. This wastes time and energy on the CPU, using a mutable object the right solution (e.g.
There are other situations when mutable objects are desirable. For example passing mutable objects into methods lets you collect multiple results without jumping through too many syntactic hoops. Another example is sorting and filtering: of course, you could make a method that takes the original collection, and returns a sorted one, but that would become extremely wasteful for larger collections. (From dasblinkenlight's answer on Stack Overflow) #9. Constructor of Super and Sub This compilation error occurs because the default super constructor is undefined. In Java, if a class does not define a constructor, compiler will insert a default no-argument constructor for the class by default. If a constructor is defined in Super class, in this case Super(String s), compiler will not insert the default no-argument constructor. This is the situation for the Super class above. The constructors of the Sub class, either with-argument or no-argument, will call the no-argument Super constructor. Since compiler tries to insert super() to the 2 constructors in the Sub class, but the Super's default constructor is not defined, compiler reports the error message. To fix this problem, simply 1) add a Super() constructor to the Super class like
, or 2) remove the self-defined Super constructor, or 3) add #10. "" or Constructor? String can be created by two ways:
What is the difference? The following examples can provide a quick answer:
For more details about how they are allocated in memory, check out Create Java String Using ” ” or Constructor?. |
|