This article mainly organizes common Java interview questions for your reference. The specific content is as follows
1. The difference between sleep and wait in Java
① These two methods come from different classes, sleep comes from Thread class, and wait comes from Object class.
sleep is a static class method of Thread. Whoever calls it goes to sleep. Even if b's sleep method is called in thread a, it is actually still going to sleep. To make thread b sleep, you have to call sleep in the code of b.
② Lock: The most important thing is that the sleep method does not release the lock, while the wait method releases the lock, so that other threads can use synchronous control blocks or methods.
sleep does not transfer system resources; wait is to enter the thread waiting pool to wait, transfer system resources, and other threads can occupy the CPU. Generally, wait will not add a time limit, because if the wait thread runs in insufficient resources, it will be useless to come out again. You have to wait for other threads to call notify/notifyAll to wake up all threads in the waiting pool before entering the ready queue and waiting for the OS to allocate system resources. sleep(milliseconds) can be specified to automatically wake it up. If the time is less than time, you can only call interrupt() to force interrupt.
The function of Thread.sleep(0) is to "trigger the operating system to re-compete CPU once again".
③ Scope of use: wait, notify and notifyAll can only be used in synchronization control methods or synchronization control blocks, while sleep can be used anywhere.
synchronized(x){ x.notify() // or wait() }2. The difference between HashMap and HashTable in Java
① Historical reasons: Hashtable is given to the old Dictonary class. HashMap is an implementation of the Map interface introduced by Java 1.2.
② HashMap allows empty key-value pairs, while HashTable does not
③ HashTable synchronization, while HashMap asynchronous is more efficient than HashTable
3. Please briefly describe the difference between throw and throws in exceptions
① throw represents an action, which means that an exception is thrown; throws represents a state, which means that the method may have an exception thrown ② throw is used in method implementation, while throws is used in method declaration ③ throw can only be used to throw one exception, while throws can throw multiple exceptions
4. The difference between memory overflow and memory leakage
Memory overflow out of memory refers to the fact that when a program applies for memory, there is no sufficient memory space for it to use, and out of memory appears; for example, if an integer is applied for but it can save long, it is memory overflow.
Memory leak memory leak refers to the program's inability to free up the memory space that has been applied for after applying for memory. The harm of a memory leak can be ignored, but the consequences of memory leak accumulation are very serious. No matter how much memory is, it will be occupied sooner or later.
Memory leak will eventually lead to out of memory!
Memory overflow means that the memory you require to allocate exceeds what the system can give you, and the system cannot meet the needs, so an overflow occurs.
Memory leaks are when you apply to the system to allocate memory for use (new), but after use, it does not return (delete). As a result, you can no longer access the memory you applied for (maybe you lost its address), and the system cannot allocate it to the required program again. If you use all the methods to fill a plate, you can only hold 4 fruits. If you fill 5, you will fall to the ground and you can't eat it. This is overflow! For example, if the stack is full, it will inevitably cause space overflow when the stack is full, which is called overflow. If the stack is empty, it will also cause space overflow when the stack is empty, which is called underflow. It means that the allocated memory is not enough to put down the sequence of data items, which is called memory overflow.
Classification in the way that occurs, memory leaks can be divided into 4 categories:
① Frequent memory leaks. The code that occurs with a memory leak will be executed multiple times, and each time it is executed, it will cause a piece of memory to leak.
② Occasional memory leak. Code that occurs with memory leaks will only occur in certain specific environments or operations. Regular and occasional are relative. For a specific environment, occasionality may become frequent. Therefore, the test environment and testing methods are crucial to detecting memory leaks.
③ One-time memory leak. The code that occurs with a memory leak will only be executed once, or due to algorithmic defects, there will always be only one piece of memory leak. For example, memory is allocated in the constructor of the class, but the memory is not released in the destructor, so memory leaks only happen once.
④ Implicit memory leak. The program continuously allocates memory during operation, but does not release the memory until it is finished. Strictly speaking, there is no memory leak here because the program ultimately frees all requested memory. But for a server program, it takes several days, weeks, or even months to run, and not freeing memory in time can also lead to the end of exhausting all memory in the system. So, we call this type of memory leak an implicit memory leak.
From the perspective of users using programs, memory leaks themselves will not cause any harm. As an ordinary user, you cannot feel the existence of memory leaks at all. What is really harmful is the accumulation of memory leaks, which will eventually consume all the memory in the system. From this perspective, one-time memory leaks are not harmful because they do not accumulate, while implicit memory leaks are very harmful because they are more difficult to detect than frequent and occasional memory leaks.
5. The difference between String, StringBuffer and StringBuilder
①Variable and immutable
String class uses a character array to save strings, as follows: Because there is a "final" modifier, you can know that string objects are immutable.
private final char value[];
Both StringBuilder and StringBuffer are inherited from the AbstractStringBuilder class. In AbstractStringBuilder, character arrays are used to save strings. As follows, it can be seen that both objects are mutable.
char[] value;
② Is it multi-threaded and safe
Objects in String are immutable, so they can be understood as constants, which are obviously thread-safe.
AbstractStringBuilder is a public parent class of StringBuilder and StringBuffer, which defines some basic operations of strings, such as expandCapacity, append, insert, indexOf and other public methods.
StringBuffer has a synchronization lock on the method or a synchronization lock on the called method, so it is thread-safe. See the following source code:
public synchronized StringBuffer reverse() { super.reverse(); return this; } public int indexOf(String str) { return indexOf(str, 0); //There is a public synchronized int indexOf(String str, int fromIndex) method} StringBuilder does not add synchronization locks to the method, so it is non-thread-safe.
③StringBuilder and StringBuffer in common
StringBuilder and StringBuffer have public parent classes AbstractStringBuilder (abstract class).
One of the differences between abstract classes and interfaces is that some public methods of subclasses can be defined in abstract classes. Subclasses only need to add new functions and do not need to repeat the existing methods; while interfaces only define methods and constants.
The methods of StringBuilder and StringBuffer will call public methods in AbstractStringBuilder, such as super.append(...). It's just that StringBuffer will add synchronized keyword to the method and perform synchronization.
Finally, if the program is not multithreaded, then using StringBuilder is more efficient than StringBuffer.
6. The difference between arrays and linked lists
Both belong to a data structure
From the logical structure:
① The array must define a fixed length (number of elements) in advance, and cannot adapt to the dynamic increase and decrease of data. When the data increases, the number of elements may exceed the originally defined; when the data decreases, memory waste will be caused; the array can be directly accessed according to the subscript.
② The linked list is dynamically stored and allocated, which can adapt to the dynamic increase and decrease of data, and can easily insert and delete data items. (When inserting and deleting data items in the array, you need to move other data items, which is very cumbersome) The linked list must find the next element according to the next pointer.
From the memory storage:
① (static) arrays allocate space from the stack, which is convenient and fast for programmers, but has little freedom.
② The linked list allocates space from the heap, which has a lot of freedom but is more troublesome to apply for management.
From the above comparison, we can see that if you need to quickly access data and rarely or not insert and delete elements, you should use an array; on the contrary, if you need to insert and delete elements frequently, you need to use a linked list data structure.
7. The difference between ArrayList and LinkedList
①ArrayList implements a data structure based on dynamic arrays, and LinkedList is based on a data structure based on a linked list.
② For random access to get and set, ArrayList feels better than LinkedList because LinkedList needs to move the pointer.
③For the addition and deletion operations add and remove, LinedList has a relatively advantage because ArrayList needs to move data.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.