Java programming ideas I have read these days

Source: Internet
Author: User

When I came here for an internship, I was prepared to read a few more books, the first of which was "Java programming ideas". Later I will read some books such as "code Daquan" and "Introduction to algorithms.

However, by reading Java programming ideas, I have gained a lot. Now I have a deeper understanding of Java. Every time I have a problem, will carefully study the JDK source code, and other issues thoroughly studied it feels very good, this website (http://www.songtao.me) is also my work during the internship, many of the blogs are also summarized by reading this book.

Let me take two simple examples:

Source code analysis of vector and arraylist:

Today I have studied the source code of vector and arraylist, and deepened my understanding of these two classes.
The list interface implements three classes: arraylist, vector, and sorted list. The sort list is not much said. It is generally used to keep the data insertion order. Arraylist and vector are implemented using arrays. There are three main differences:
1. the vector is multi-threaded and secure, but the arraylist is not. We can see from the source code that many methods in the vector class are modified using synchronized, as a result, the efficiency of vector cannot be compared with that of arraylist;
2. Both use linear continuous space storage elements, but when the space is insufficient, the two classes are added in different ways. Many netizens say that vector doubles the original space, arraylist adds 50% of the original space, which is similar to this. However, we can see a little problem from the source code and analyze it from the source code later.
3. The growth factor can be set for vector, but not for arraylist. I didn't understand what the increment factor was when I first looked at it. But I understood it by comparing the two source codes, let's take a look at the constructor methods of the two classes:

Arraylist has three constructor Methods :'

Public arraylist (INT initialcapacity) // construct an empty list with the specified initial capacity. Public arraylist () // construct an empty list with an initial capacity of 10. Public arraylist (collection <? Extends E> C) // construct a list of elements that contain the specified collection.

Vector has four constructor methods:

Public vector () // uses the specified initial capacity and the Capacity Increment equal to zero to construct an empty vector. Public vector (INT initialcapacity) // construct an empty vector to increase the size of its internal data array. Its standard capacity is increased to zero. Public vector (collection <? Extends E> C) // construct a public vector (INT initialcapacity, int capacityincrement) containing elements in the specified collection. // construct an empty vector using the specified initial capacity and capacity increment.

Vector has one more constructor than arraylist. That is, public vector (INT initialcapacity, int capacityincrement) constructor. capacityincrement is the capacity growth factor, arraylist does not exist.

Add the following two classes for source code analysis (jdk1.7 ):

Source code for adding the arraylist class: Public Boolean add (E) {ensurecapacityinternal (size + 1); // increments modcount !! Elementdata [size ++] = E; return true;} private void ensurecapacityinternal (INT mincapacity) {modcount ++; // overflow-conscious code // after an element is added, the size of the new container is greater than the container capacity, so the value cannot be saved. You need to expand the space if (mincapacity-elementdata. length> 0) grow (mincapacity);} private void grow (INT mincapacity) {// overflow-conscious codeint oldcapacity = elementdata. length; int newcapacity = oldcapacity + (oldcapacity> 1); // The expanded space is increased by 50% (that is, the original space ). 1.5 times) if (newcapacity-mincapacity <0) // if the container is not enough after expansion, set mincapacity to the container size newcapacity = mincapacity; if (newcapacity-max_array_size> 0) // If the expanded container is too large, run hugecapacitynewcapacity = hugecapacity (mincapacity); // mincapacity is usually close to size, so this is a win: elementdata = arrays. copyof (elementdata, newcapacity);} source code for adding the Vector class: Public synchronized Boolean add (E) {modcount ++; ensureca Pacityhelper (elementcount + 1); elementdata [elementcount ++] = E; return true;} private void ensurecapacityhelper (INT mincapacity) {// overflow-conscious codeif (mincapacity-elementdata. length> 0) grow (mincapacity);} private void grow (INT mincapacity) {// overflow-conscious codeint oldcapacity = elementdata. length; int newcapacity = oldcapacity + (capacityincrement> 0 )? Capacityincrement: oldcapacity);/** this expansion requires a judgment: If the size increment Initialization is not 0, it is initialized using the public vector (INT initialcapacity, int capacityincrement) constructor, the capacity to be expanded is (oldcapacity + capacityincrement), that is, the original capacity plus the Capacity Increment value. If the capacity increment is not set, the capacity after expansion is (oldcapacity + oldcapacity ), is twice the original capacity. **/If (newcapacity-mincapacity <0) newcapacity = mincapacity; If (newcapacity-max_array_size> 0) newcapacity = hugecapacity (mincapacity); elementdata = arrays. copyof (elementdata, newcapacity );}


Through analysis, I should be able to understand it now. Now I can understand so much!

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.