Java Interview Basics Summary (i)

Source: Internet
Author: User
Tags stringbuffer

1, the size of the nine basic data types, and their encapsulation class

Java provides nine basic data types: Boolean, Byte (1), char (2), short (2), int (4), Long (8), float (4), double (8), void
And their encapsulation classes: Boolean, Byte, Character, short, Integer, Long, Float, Double, Void

2. Can switch use string to do parameters?

You can use string to make arguments after Java7

3. The difference between equals and = =

"= =" is a comparison of the two objects of the comparison, equals in the object class is based on "= =" Implementation, so the Equals method based on the user's needs to customize to achieve the comparison of similar objects

Other classes provided in Java basically implement the replication of equals

4. What are the common methods for object?

The object class has 12 member methods that can be divided into the following types of use
1, constructor
The 2,hashcode and Equale functions are used to determine if the object is the same
3,wait (), wait (long), wait (Long,int), notify (), Notifyall ()
4,tostring () and GetClass,
5,clone ()
6,finalize () is used for garbage collection

5, four kinds of references in Java, soft and weak, used to the scene

1. Strong references

Strong references are not recycled by GC, and there is no actual corresponding type in Java.lang.ref, and the most work-contact is a strong reference. Object obj = new Object (); The obj reference here is a strong reference. If an object has a strong reference, it is similar to essential necessities, and the garbage collector will never recycle it. When there is not enough memory space, the Java virtual Machine prefers to throw a outofmemoryerror error, which causes the program to terminate abnormally, and does not rely on random recycling of strongly referenced objects to resolve out-of-memory issues.

2. Weak references (WeakReference)

If an object has only a weak reference, like a dispensable product, the difference between a weak reference and a soft reference is that an object with only a weak reference has a shorter life cycle, and when the garbage collection thread scans the area it governs, once a weakly referenced object is found, regardless of whether the current memory space is sufficient or not, Will reclaim its existence, but the garbage collector is a low-priority thread, so it is not always possible to quickly discover those objects that have references

3. Soft Reference (SoftReference)

If an object has only a soft reference, it is similar to an optional item, if the memory space is sufficient, the garbage collector will not reclaim the memory of these objects, as long as the garbage collector does not reclaim it, the object can be used by the program, soft references can be implemented to implement memory-sensitive cache, soft references to the reference queue ( Referencequeue), if the soft-referenced object is garbage collected, the Java Virtual machine will add the soft reference to the associated reference queue

4. Virtual references (phantomreference)

"Virtual Reference" as the name implies, is the form of a dummy, and several other references are different, it does not determine the life cycle of an object, if an object only has a virtual application, then he and without any reference, can be garbage collection at any time, virtual reference is mainly used to track the object garbage collection activities, One difference between a virtual reference and a soft reference and a weak reference is that a virtual reference must be used in conjunction with a reference queue, and when the garbage collector is ready to reclaim an object, if it finds that he has a virtual reference, it adds the virtual reference to the reference queue associated with it before reclaiming the object's memory. The program can determine whether a virtual reference is added to the reference queue to see if the referenced object is going to be garbage collected, and if the program discovers that a virtual reference is added to the reference queue, it can take the necessary action before the memory of the referenced object is recycled.

6, the role of Hashcode

Can be used to compare objects, but not necessarily reliable, equals the same two objects, the hashcode must be the same, but the contrary is not necessarily the same

Used to determine the position of an object in a hash structure by hashcode in a hash structure such as Hashtable, HASHMAP, etc.

7, ArrayList, LinkedList, vector of the difference

1.ArrayList is the most commonly used list implementation class, implemented internally by an array, which allows for fast random access to elements. The disadvantage of an array is that there can be no interval between each element, and when the array size does not meet the need to increase storage capacity, it is necessary to copy the data of the array into the new storage space. When inserting or deleting elements from the middle of a ArrayList, it is necessary to copy, move, and cost the array. Therefore, it is suitable for random lookups and traversal, and is not suitable for insertions and deletions.

2.Vector, like ArrayList, is also implemented through arrays, except that it supports thread synchronization, where only one thread can write vectors at a time, avoiding inconsistencies caused by simultaneous writing of multiple threads, but achieving synchronization requires a high cost, so Accessing it is slower than accessing ArrayList.

3.LinkedList is used to store data in a linked list structure, which is very suitable for the dynamic insertion and deletion of data, and the random access and traversal speed is relatively slow. In addition, he provides methods that are not defined in the list interface, specifically for manipulating the header and footer elements, and can be used as stacks, queues, and bidirectional queues.

4, when the capacity is not enough to expand, ArrayList is the expansion of 50%,vector is 100%

5, because the LinkedList is a chain structure, so the performance in the insertion and deletion is better than ArrayList, in terms of query ArrayList

8. The difference between String, StringBuffer and StringBuilder

string literal constant
StringBuffer string variable (thread safe)
StringBuilder string variable (non-thread safe)

The difference between the three classes is mainly in two aspects, that is, the speed of operation and the thread safety of the two aspects

1. First say the speed of operation, or the speed of execution, in this respect the speed of operation is: StringBuilder > StringBuffer > String

The slowest reason for string:

Strings are string constants, and StringBuilder and StringBuffer are string variables, that is, once a string object is created, the object is immutable, but the latter object is a variable and can be changed. Each time the string type is changed, it is equivalent to generating a new string object, and then pointing the pointer to the new string object, so it is best not to use string to change the content of the strings, because each generation of the object will have an impact on the system performance, Especially when there are no more reference objects in memory, the JVM's GC will start to work, and the speed will be quite slow.

2. Again, thread-safe, thread-safe, StringBuilder threads are unsafe, and stringbuffer is thread-safe

If a StringBuffer object is used by multiple threads in a string buffer, many methods in StringBuffer can have the Synchronized keyword, so that the thread is secure, but the StringBuilder method does not have the keyword. Therefore, there is no guarantee of thread safety, there may be some wrong operation. So if the operation is multi-threaded, then use StringBuffer, but in the case of single-threaded, it is recommended to use a faster StringBuilder.

9. Features and usage of MAP, Set, List, Queue, stack
    • Map

The map is a key-value pair, and the key is the only one that cannot be duplicated, and a key corresponds to a value that can be repeated.
TreeMap can guarantee the order, HashMap does not guarantee the order, namely is unordered.
The key and value can be extracted separately in map, where the keyset () method can extract all keys from a set. The values () method can extract all values from a map into a single set.

    • Set

A collection that contains no duplicate elements, with a maximum of one null element in the set
Only single traversal can be implemented with iterator, there is no synchronization method in set.

    • List

An ordered, repeatable collection.
Delete elements can be added anywhere.
Implement one-way traversal with iterator, and also use Listiterator to implement bidirectional traversal

    • Queue

The queue complies with the FIFO principle.
Avoid the Add () and remove () methods as much as possible, but instead use the offer () to add elements and remove elements using poll (), which has the advantage of being able to determine success by returning values.
LinkedList implements the queue interface.
A queue typically does not allow the insertion of a null element.

    • Stack

Stack adheres to the LIFO principle.
Stack inherits from Vector.
It extends the class vector by five operations, allowing the vector to be treated as a stack, which provides the usual push and pop operations, and the Peek () method that takes the stack vertex, the empty method for testing the stack, and so on.

    • Usage

If you are involved in operations such as stacks, queues, etc., we recommend using the list
For quick insertion and deletion of elements, it is recommended to use LinkedList
If you need a quick random access element, we recommend that you use ArrayList

10. The difference between HashMap and Hashtable

1, Hashtable is inherited dictionary class, and HashMap is the implementation of the map interface

2, HashMap is thread insecure, Hashtable is thread-safe

3. HashMap can use NULL as key and value

The HashMap can be synchronized using the following statement:
Map m = Collections.synchronizemap (HASHMAP);

11. The difference between HashMap and Concurrenthashmap

Concurrenthashmap is thread-safe, using the lock separation technology, the internal divided into several segments (by default, divided into 16 segments), through the hashcode of key to determine in which paragraph, the operation of the operation of the segment to lock, so by default, If ideal, you can have 16 threads working on a concurrenthshmap at the same time.

12, TreeMap, HashMap, Linkedhashmap difference

The TreeMap implements the Sortmap interface and is able to sort its saved records according to the key.
The default is to sort the key values in ascending order, or you can specify a sort comparer, and when traversing treemap with iterator, the resulting records are sorted out.

HashMap is the most commonly used map, which stores data according to the hashcode value of the key, which can be obtained directly from the key, with fast access speed. When traversing, the order in which the data is obtained is completely random.
HashMap allows a maximum of one record's key to be null, and the value to allow multiple records to be null.
HashMap does not support thread synchronization (that is, multiple threads can write HashMap at any one time), which can result in inconsistent data. If synchronization is required, you can use the collections Synchronizedmap method to make the HashMap capable of synchronizing or using Concurrenthashmap.
Hashtable is similar to HashMap, which inherits from the dictionary class. The difference is that it does not allow the record key or value to be null, it supports thread synchronization (that is, only one thread can write hashtable at any one time), and therefore causes the hashtable to be slower to write.

Linkedhashmap saves the order in which records are inserted, and when traversing linkedhashmap with iterator, the first records must be inserted first. They can also be constructed with parameters, sorted by the number of applications.
The traversal will be slower than HashMap, but there is an exception: when the HashMap capacity is large and the actual data is small, the traversal may be slower than linkedhashmap. Because the Linkedhashmap traverse speed is only related to the actual data, and the capacity is independent, and HashMap's traverse speed is related to his capacity

13, collection package structure, and the difference between collections

Collection is a top-level interface of a collection class, and its direct inheritance interface has list and set

Collections is a tool class/helper class for the collection class, which provides a series of static methods for sorting, searching, and line Cheng of elements in the collection.

14, Try/catch Finally,try inside have retrun,finally also will execute?

A return in the try will execute the return after finally, and if finally return, the return in the try will not be executed.

Finally can have an effect on the results of non-underlying data types in a try, and the underlying data types do not affect

15, exception and ERRO package structure, oom you have encountered what situation, SOF you have encountered what situation

50951617

Java Interview Basics Summary (i)

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.