13 Crucial Java Practices Every Developer Should Follow

This article examines fifteen common Java pitfalls and best‑practice guidelines—ranging from property copying and date formatting to HashMap sizing, thread‑pool creation, collection handling, logging, and serialization—explaining why each recommendation exists and how it impacts performance and safety.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
13 Crucial Java Practices Every Developer Should Follow

Why prohibit using Apache BeanUtils for property copying?

Many property‑copy utilities exist, such as Spring BeanUtils, Cglib BeanCopier, Apache BeanUtils, Apache PropertyUtils, and Dozer. The Java Development Handbook advises against Apache BeanUtils because it can cause hidden bugs and performance issues.

Why must date formatting use 'y' for year instead of 'Y'?

When formatting dates in Java, using SimpleDateFormat with the correct pattern is essential. Using 'Y' can lead to subtle bugs because it represents week‑year rather than calendar year, potentially causing major errors in date calculations.

What is the null‑pointer issue with the ternary operator?

The handbook warns that the ternary operator can trigger an automatic unboxing of a null wrapper type, resulting in a NullPointerException. Developers should guard against null values before using the operator.

Why recommend initializing HashMap capacity?

Setting an initial capacity reduces the number of costly rehash operations as the map grows, improving performance.

What is an appropriate initial capacity for a HashMap?

HashMap expands when its size exceeds threshold = loadFactor * capacity. Choosing a capacity that anticipates the expected number of entries avoids repeated resizing and the associated overhead of rebuilding the hash table.

Why avoid using Executors to create thread pools?

Although Executors provides convenient factory methods, they hide important configuration details (such as thread‑factory and rejection policy) that can lead to sub‑optimal or unsafe thread‑pool behavior. Directly constructing ThreadPoolExecutor is recommended.

Why be cautious with ArrayList.subList ?

subList

returns a view backed by the original list; structural modifications to the original list can cause ConcurrentModificationException. Understanding this relationship prevents unexpected runtime errors.

Why not concatenate strings with '+' inside loops?

The '+' operator creates a new StringBuilder each iteration, leading to excessive object creation and memory overhead. Using a single StringBuilder outside the loop is far more efficient.

Syntactic sugar makes code concise and readable but can hide performance costs.

Why forbid remove/add operations inside a foreach loop?

Modifying a collection while iterating with the enhanced for‑loop triggers a ConcurrentModificationException. Use an explicit iterator and its remove method instead.

Why avoid direct use of logging APIs like Log4j or Logback?

Directly calling logging APIs couples code to a specific framework, making future migration difficult and potentially bypassing configuration best practices such as asynchronous logging or proper level management.

Why should SimpleDateFormat not be defined as a static variable?

SimpleDateFormat

is not thread‑safe; sharing a static instance across threads can cause incorrect parsing/formatting and unpredictable results.

Why avoid naming a boolean variable isSuccess ?

Boolean naming conventions recommend using clear, descriptive names that convey intent without ambiguity; improper naming can lead to misunderstandings in API contracts.

Why should the serialVersionUID field not be modified?

serialVersionUID

ensures compatibility between serialized forms of a class. Changing its value breaks deserialization of previously persisted objects.

Why be cautious with inheritance?

Inheritance can create tight coupling and fragile hierarchies. Prefer composition over inheritance to achieve more flexible and maintainable designs.

Why prohibit using COUNT(column) or COUNT(constant) instead of COUNT(*) ?

Counting a specific column or constant can lead to different execution plans and may not use the most efficient index. COUNT(*) is generally optimized by the database engine for row counting.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSQLconcurrencyloggingCollections
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.