Recent gains in learning Java

Source: Internet
Author: User

1. Not only does the system take time to create objects, but it takes time to recycle and process these objects, so you should try to avoid too many frequently created objects that can be used to replace objects with basic data types or arrays;

2. When an object is defined as being referenced by a static variable, the memory occupied by the object is not recycled, so avoid using static variables as much as possible;

3. The parameters passed when the method is called and the temporary variables created in the call are saved in the stack and are faster. Other variables, such as static variables, instance variables, and so on, are created in the heap and are slower. Therefore, local variables should be used as much as possible;

4. Typically, the StringBuffer constructor creates a character array of the default size (16), which, when used, will reallocate memory, create a larger array, copy the original array, and discard the old array. If you specify the size when creating the StringBuffer, this avoids the situation of auto-growth when the capacity is not enough, so the capacity of stringbuffer should be determined as far as possible;

5. Two-D data occupies much more memory space than one-dimensional array, so we should avoid using two-dimensional array;

6. When creating an exception, you need to collect a stack trace that describes where the exception was created. Building these stack traces requires a snapshot of the runtime stack, which is a very expensive part. When you need to create a Exception, the stack and the stack operation are temporarily stopped. That is, creating an exception requires a lot of overhead. So be cautious with exceptions.

Recent gains in learning Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.