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 should be paid attention to in Java programming

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what to pay attention to in Java programming". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Try to use singletons on appropriate occasions

The use of singletons can reduce the burden of loading, shorten the loading time, and improve the efficiency of loading, but it is not applicable to singletons everywhere. To put it simply, singletons are mainly applicable to the following three aspects:

*, control the use of resources, and control the concurrent access of resources through thread synchronization

Second, control the generation of examples in order to achieve the purpose of saving resources

Third, control data sharing and let multiple unrelated processes or threads communicate without establishing a direct association.

two。 Try to avoid the random use of static variables

You know, when an object is defined as a reference to the stataic variable, gc usually does not reclaim the memory occupied by the object, as shown in

Public class A {static B b = new B ();}

At this point, the life cycle of the static variable b is synchronized with class A, and if class A does not unload, then the b object will reside in memory until the program terminates.

3. Try to avoid creating Java objects too often.

Try to avoid new objects in frequently called methods, loops, because the system not only takes time to create objects, but also takes time to garbage collect and process these objects, within our control, * limited reuse objects, * can replace objects with basic data types or arrays.

4. Use final modifiers whenever possible

Classes with final modifiers are not derivable. In the core API of Java, there are many examples of using final, such as java.lang.String. Specifying final for the String class prevents the consumer from overriding the length () method. In addition, if a class is final, then all methods of that class are final. The Java compiler will look for opportunities to inline (inline) all final methods (this is related to the specific compiler implementation). This can improve performance by an average of 50%.

5. Use local variables as much as possible

The parameters passed when the method is called and the temporary variables created in the call are stored in the Stack, which is faster. Other variables, such as static variables, instance variables, and so on, are created in the Heap and are slow.

6. Try to deal with the use of both packaging types and basic types.

Although the wrapper type and the basic type can be converted to each other in the process of use, the memory areas generated by them are completely different, the basic type data generation and processing are processed in the stack, and the wrapper type is an object. Is to generate an instance in the heap.

In the collection class object, the wrapper type is applicable to the processing needed for the object, and the basic type is advocated for other processing.

7. Use synchronized cautiously and minimize synchronize

As we all know, the realization of synchronization takes a lot of system overhead as a price, and may even cause deadlock, so try to avoid unnecessary synchronization control. When the synchronize method is called, the current object is locked directly, and other threads cannot call other methods of the current object until the method is executed. So the method of synchronize is as small as possible, and you should try to use method synchronization instead of code block synchronization.

8. Try to use StringBuilder and StringBuffer for string concatenation

I won't say much about this.

9. Try not to use the finalize method

In fact, it is a very bad choice to clean up resources in the finalize method. Because of the heavy workload of GC, especially when reclaiming Young generation memory, it will mostly cause applications to pause, so choosing to use the finalize method for resource cleaning will lead to a greater burden on GC and less efficient programs.

10. Try to use basic data types instead of objects

String str = "hello"

The above method creates a "hello" string, and JVM's character cache pool also caches the string

String str = new String ("hello")

At this time, in addition to creating strings, the bottom layer of the String object referenced by str also contains a char [] array. This char [] array stores, in turn, the hmeme _ LJI _ LJO.

11. Single thread should use HashMap and ArrayList as much as possible.

HashTable, Vector and so on use synchronization mechanism, which degrades the performance.

twelve。 Create HashMap as reasonably as possible

When you want to create a larger hashMap, take full advantage of another constructor

Public HashMap (int initialCapacity, float loadFactor)

To prevent HashMap from performing hash refactoring many times, capacity expansion is a very performance-consuming thing. By default, initialCapacity is only 16, while loadFactor is 0.75. how much capacity you need and you can accurately estimate the size of * *, and the same is true for Hashtable,Vectors.

13. Minimize the double calculation of variables

Such as

For (int iTuno Tinci 2; int num = a > > 3)

However, it is important to note that comments should be added when using shift, because the shift operation is not intuitive and difficult to understand.

17. Try to use shift instead of 'axib' operation

Similarly, for'* 'operations, using shift operations will be faster and more efficient

Such as

Int num = a * 4; int num = a * 8

Should be changed to

Int num = a

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