Common java and Interview Questions & lt; 1 & gt;

Source: Internet
Author: User

1. Differences between String and StringBuffer
String to operate on a String of characters. Immutable class.
StringBuffer also operates on a string of characters, but it can be a variable class.
String:
Yes. The object is not of the original type.
An unchangeable object. Once created, its value cannot be modified.
To modify an existing String object, create a new object and save the new value.
String is a final class, which cannot be inherited.
StringBuffer:
It is a mutable object. objects are not re-created as strings when modified.
It can only be created by constructors,
StringBuffer is more efficient than String in String connection operations:
 
2. Do I use run () or start () to start a thread ()?
When a thread is started, the start () method is called to make the virtual processor represented by the thread in a running state, which means that it can be scheduled and executed by JVM. And
It does not mean that the thread will run immediately. The run () method can generate the exit sign to stop a thread.
 
3. Differences between final and finally.

If final modifies a basic type, it indicates that the value assigned to the variable is immutable, that is, it is a constant. If final modifies an object, it indicates
The reference assigned to this variable is immutable,
What cannot be changed is the reference saved by this variable, and it is not the object pointed to by this reference. In the second case, final has the same meaning as in the first case. Actual
For the first two cases, there is a more appropriate description of the meaning of final, that is, if a variable or method parameter is modified by final, it can only be assigned
Value once, but the default value set by the Java Virtual Machine for the variable is not recorded as a value assignment.
Finally usage. It can only be used in a try/catch statement with a block, indicating that the statement is always executed.

4. What is the difference between List and Map?
Different Data Structures;
List is a collection of objects, allowing repeated objects.
Map is a set of key-value pairs. Duplicate keys are not allowed.

5. Advantages of PreparedStatement in JDBC compared with Statement
1. relatively secure, preventing SQL Injection
PreparedStatement is used to input an SQL Statement before execution. The Statement is the opposite. It is used to input an SQL Statement during execution. The difference is that PreparedStatement can assign values to parameters before the SQL statement is executed after the SQL statement is passed in. This avoids the security problems caused by common concatenated SQL string statements, the preparation and execution of SQL statements are completed in two statements, which improves the efficiency of statement execution.
2. The pre-compilation function is available, and the same operation of batch data is more efficient.
The PreparedStatement interface in java inherits Statement, and PreparedStatement inherits all functions of Statement. The PreparedStatement object has been pre-compiled, so its execution speed is faster than the Statement object.
6. What are the collection classes you know? What are the main methods?
List Interface
List is an ordered Collection, which can be used to precisely control the insert position of each element. You can use an index (the position of an element in the List, similar to an array subscript) to access the elements in the List, which is similar to an array in Java.
Unlike the Set mentioned below, the List can have the same element.
In addition to the iterator () method required for the Collection interface, List also provides a listIterator () method to return a ListIterator interface. Compared with the standard Iterator interface, ListIterator has some more add () you can add, delete, and set elements to traverse forward or backward.
Common classes that implement the List interface include the List, ArrayList, Vector, and Stack.
Sort list class
The listlist interface allows null elements. In addition, the values list provides additional get, remove, and insert methods at the beginning or end of the values list. These operations enable the queue list to be used as a stack, queue, or two-way queue (deque ).
Note that the synchronized list method is not available. If multiple threads access a List at the same time, they must implement access synchronization by themselves. One solution is to construct a synchronized List when creating a List:
List list = Collections. synchronizedList (new Collections List (...));
ArrayList class
ArrayList implements an array of variable sizes. It allows all elements, including null. ArrayList is not synchronized.
Size, isEmpty, get, set method running time is constant. However, the overhead of the add method is the constant of the allocation. It takes O (n) to add n elements. The running time of other methods is linear.
Each ArrayList instance has a Capacity, that is, the size of the array used to store elements. This capacity can automatically increase with the addition of new elements, but the growth algorithm is not defined. When a large number of elements need to be inserted, you can call the ensureCapacity method before insertion to increase the ArrayList capacity to improve the insertion efficiency.
Like the synchronized list, ArrayList is also non-synchronous (unsynchronized ).
Vector
The Vector is very similar to the ArrayList, but the Vector is synchronized. Although the Iterator created by Vector is the same interface as the Iterator created by ArrayList, because Vector is synchronous, when an Iterator is created and in use, another thread changes the state of the Vector (for example, adding or deleting some elements). When calling the Iterator method, ConcurrentModificationException is thrown. Therefore, this exception must be caught.
Stack
Stack inherits from Vector to implement a post-import, first-out Stack. Stack provides five additional methods to make the Vector used as a Stack. The basic push and pop methods also include the elements of the peek method to get the top of the stack. The empty method tests whether the stack is empty. The search method checks the position of an element in the stack. The Stack is empty after being created.
Set Interface
Set is a Collection that does not contain repeated elements, that is, the two elements e1 and e2 both have e1.equals (e2) = false, and Set has a maximum of null elements.
Obviously, the Set constructor has a constraint that the imported Collection parameter cannot contain repeated elements.
Note: You must be careful when operating Mutable objects ). If a variable element in a Set changes its state, Object. equals (Object) = true may cause some problems.
Map Interface
Note that Map does not inherit the Collection interface. Map provides the key ing between key and value. A Map cannot contain the same key, and each key can only Map one value. The Map interface provides three sets of views. The Map content can be treated as a set of keys, a set of values, or a set of key-value ing.
Hashtable class
Hashtable inherits the Map interface and implements a key-value ing hash table. Any non-null object can be used as a key or value.
Put (key, value) is used for adding data, and get (key) is used for retrieving data. The time overhead of these two basic operations is constant.
Hashtable uses the initial capacity and load factor parameters to adjust the performance. Generally, the default load factor 0.75 achieves a better balance between time and space. Increasing the load factor can save space, but the corresponding search time will increase, which affects operations such as get and put.
A simple example of Hashtable is as follows: Put 1, 2, 3 into Hashtable, and their keys are "one", "two", and "three ":
Hashtable numbers = new Hashtable ();
Numbers. put ("one", new Integer (1 ));
Numbers. put ("two", new Integer (2 ));
Numbers. put ("three", new Integer (3 ));
To retrieve a number, such as 2, use the corresponding key:
Integer n = (Integer) numbers. get ("two ");
System. out. println ("two =" + n );
As the key object is determined by calculating its hash function, any object used as the key must implement the hashCode and equals methods. The hashCode and equals Methods inherit from the root class Object. If you use a custom class as the key, be very careful. According to the definition of the hash function, if the two objects are the same, that is, if obj1.equals (obj2) = true, their hashCode must be the same, but if two objects are different, their hashCode is not necessarily different. If the hashCode of two different objects is the same, this phenomenon is called a conflict. A conflict will increase the time overhead for operating the hash table. Therefore, the hashCode () method should be defined as much as possible to speed up the operation of the hash table.
If the same object has different hashCode, operations on the hash table will produce unexpected results (the expected get method returns null). To avoid this problem, you only need to remember one: the equals and hashCode methods must be rewritten at the same time, instead of writing only one of them.
Hashtable is synchronous.
HashMap class
HashMap is similar to Hashtable. The difference is that HashMap is non-synchronous and allows null, that is, null value and null key ., However, when HashMap is treated as a Collection (the values () method can return the Collection), its iteration suboperation time overhead is proportional to the capacity of HashMap. Therefore, if the performance of iterative operations is very important, do not set the HashMap initialization capacity too high or the load factor too low.
WeakHashMap class
WeakHashMap is an improved HashMap that implements "weak references" to keys. If a key is no longer referenced by external entities, it can be recycled by GC.

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.