Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the suggestions for writing good code in Java programming?

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what are the suggestions for writing good code in Java programming? the content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Recently, I have been doing performance optimization of applications, and have accumulated some rules and experience in the process of review code. The purpose of these rules is simple: to write "beautiful" code.

1. The notes are as comprehensive as possible.

Comments on methods should include detailed input parameters and result descriptions, and exceptions thrown should be described in detail; comments on classes should include functional descriptions, authors, and modifiers of the class.

2. The same variable used many times is summed up as a constant.

Variables with the same value used in multiple places should be summarized as a constant as far as possible to facilitate future maintenance.

3. Execute method calls in loops as little as possible

Try to make fewer avoidable method calls in the loop, which can save the creation of the method stack. For example:

For (int iTuno System.out.println (I);}

Can be modified to:

For (int item0 int sizekeeper list.size (); I System.out.println (I);}

4. The definition of constant can be put into the interface.

In Java, only constants are allowed in the interface, so the keyword public static final can be omitted by declaring constants in the interface.

The choice of 5.rrayList and LinkedList

This problem is quite common. Programmers are usually able to evaluate list usage scenarios and then make choices based on features. The underlying ArrayList is implemented using arrays, so randomly reading data is much faster than LinkedList, while LinkedList is implemented using linked lists, adding and deleting data much faster than ArrayList.

6. String,StringBuffer and StringBuilder

This problem is also quite common. When doing string concatenation, String usually produces multiple objects and caches multiple values in a constant pool. For example:

String a = "a"; String b = "b"; a=a+b

In this case, jvm produces three objects: "a", "b" and "ab". And the performance of string concatenation is also very low. Therefore, when you usually need to do string processing, try to use StringBuffer and StringBuilder.

7. The selection of packaging class and basic type

In the code, try to use the basic data type if you can use the basic data type as the local variable type, because the variables of the basic type are stored in the stack and the variables of the wrapper class are in the heap. The stack operates much faster than the heap.

8. Assign unused variable references to null as soon as possible

Doing so helps jvm to reclaim memory faster. Of course, many people are not interested in this practice.

9. Release resources in the finally block

In a typical scenario, when using io streams, streaming should be turned off in finally regardless of whether there is an exception * *.

10. When using an Object as a key in HashMap, you should pay attention to how to distinguish whether the Object is the same or not

In the HashMap implementation of jdk, the criterion for determining whether the key of two Object types are the same is whether the hashcode is the same and the return value of the equals method. If the business needs to store two memory objects with the same data in hashmap as different key, override the hashcode and equals methods.

This is the end of the suggestions on writing good code in Java programming. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report