Java Question Analysis

Source: Internet
Author: User

& Differences:

&: In the two expressions, if the value of the previous expression is false, continue to execute the next expression.

&: If the value of the previous expression is false, the next expression is skipped.

In logical operations, the expression is false and the result is false. Therefore, the & efficiency ratio & is higher.

 

Difference between string and stringbuffer

Please see http://blog.csdn.net/yirentianran/archive/2008/09/03/2871417.aspx think the analysis is good.

 

Math. Round (4.5) and math. Round (-5.5) Results

 

Math. Round (4.5) = 5

Math. Round (-5.5) =-5

Math. Round (float f)/Math. Round (double D) gets the lOng/INT closest to the parameter. This is what we usually call rounding. Add the parameter 1/2 and then perform the modulo operation.

Equivalent to (long) math. Floor (F + 0.5f)/(INT) math. Floor (D + 0.5d)

 

Differences between arrarylist and vector

Vector and arraylist are the implementation of the list interface. They are very similar in usage and can be used to represent a set of variable object applications, and can randomly access the elements.

The vector method is synchronous and thread-safe. The arraylist method is not. Because thread synchronization must affect performance, arraylist has better performance than vector.

When the element in the vector or arraylist exceeds its initial size, the vector doubles its capacity, while the arraylist only increases by 50%. In this way, the arraylist is conducive to saving memory space.

 

Hashmap and hanshtable

Hashtable and hashmap are the implementation of map interfaces. Their performance is similar to that of vector and arraylist. For example, the hashtable method is synchronous, but hashmap is not.


Differences between arraylist and rule list

For processing a column of data items, Java provides two classes: arraylist and arraylist. The internal implementation of arraylist is based on the internal array object []. So Conceptually, it is more like an array, however, the internal implementation of The shortlist is based on a set of connected records. Therefore, it is more like a linked list structure. Therefore, they differ greatly in performance.

From the above analysis, we can see that when inserting data in front of or in the middle of the arraylist, you must move all the data after it. This will inevitably take a lot of time, when you add data to the end of a column of data rather than in the front or middle, and you need to randomly access the elements in the column, arraylist provides better performance. When accessing an element in the linked list, you must start from the end of the linked list and search for the element one by one in the connection direction until you find the required element. Therefore, when you add or delete data in front or middle of a column of data and access the elements in the column in sequence, you should use the sort list.

If, in programming, the first and second situations alternate, you can consider using a general interface such as list, without worrying about the specific implementation. In specific circumstances, its performance is guaranteed by the specific implementation.

 

Configure the initial size of the Collection class

The size of most classes in the Java Collection framework can increase accordingly as the number of elements increases. We don't seem to care about its initial size, however, if we consider the performance of the class, we must set the initial size of the Set object as much as possible, which will greatly improveCodePerformance.For example, the default initial size of hashtable is 101, and the load factor is 0.75. If the number of elements exceeds 75, it must increase the size and reorganize the elements, if you know that the exact number of elements is 110 when creating a new hashtable object, you should set its initial size to 110/0. 75 = 148. In this way, you can avoid reorganizing the memory and increasing the size.


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.