[Java Basics] common knowledge accumulation (1), java basics Accumulation

Source: Internet
Author: User

[Java Basics] common knowledge accumulation (1), java basics Accumulation

1. Difference Between = and equals: difference between null and ""

2. 15 top-level Java multi-thread Interview Questions and answers

(1) Use of join in a thread

In many cases, the main thread generates and starts a subthread. If a large number of time-consuming operations are required in the subthread, the main thread usually ends before the subthread, however, if the main thread needs to use the processing result of the sub-thread after processing other transactions, that is, the main thread needs to wait until the sub-thread completes the execution and then end, then it needs to use join () method.

(2) synchronized and Lock in the thread

(3) deadlock in the thread

(4) What are the differences between the wait and sleep methods in java?
Java thread interview questions that are frequently asked during telephone interviews. The biggest difference is that wait releases the lock while waiting, and sleep keeps holding the lock. Wait is usually used for interaction between threads, and sleep is usually used to suspend execution.

(5) Java multi-thread atomic operations

SerialNumberGenerator is probably the simplest example you can think. If you transfer data from C ++ or have experience in other low-level languages, you will think that increment is definitely an atomic operation, because it is usually implemented using CPU commands. But in JVM, increment is not an atomic operation. It involves reading and writing. So even a simple operation, multithreading is also a good choice.

public class SerialNumberGenerator {private static volatile int serialNumber = 0;public static int nextSerialNumber() {return serialNumber++;}}

The serialNumber field is volatile, so that the serialNumber field values of all threads are consistent.
Multithreading involves the primary memory and the working memory. In JVM, a primary memory is used to share data among all threads. Each thread has its own private working memory, the volatile variable indicates that it must be consistent with the main memory, and is actually the synchronization of variables.

3. Why is the Java String object immutable?

According to the source code of java. lang. String in JDK, we can find out the reason why the String type object is unchangeable. Generally, there are two reasons:
(1) When java. lang. String is implemented, all its internal member variables are modified using final to ensure that the reference values of member variables can only be modified by constructors;
(2) java. lang. when the String type is implemented, a New String object, a new byte array, or a char array will be constructed when the returned result is in the function implementation that may modify its internal storage value externally;
2nd the importance is that, if the toCharArray method of the String type can directly access the char array defined in the String type, even if the char array inside the String type is modified using final, the reference of this member variable cannot be changed, but the memory area pointed to by the reference cannot be changed. From the above two points, it is impossible to modify the Internal Attributes of java. lang. String objects externally, so as to ensure that the String objects are immutable.

4. StringBuffer and StringBuilder

When it comes to String, we have to mention two other classes in JDK that are commonly used to represent strings, StringBuffer and StringBuilder. According to the notes, StringBuffer is an old qualification. From JDK1.0, it is accompanied by Java, and StringBuilder did not appear until JDK1.5.

What StringBuffer and StringBuilder have in common:
(1) All are variable objects, and the character cache in the object will be dynamically expanded with the splicing operation;
(2) String concatenation;
(3) Importing an internal Cache during construction can reduce the number of cache extensions and significantly improve the efficiency of String concatenation;
Difference between StringBuffer and StringBuilder:
(1) The StringBuilder method is thread-insecure. from another point of view, the StringBuilder-type objects have fewer thread-synchronous operations during String concatenation, the execution efficiency has been greatly improved;
(2) The StringBuffer method adds the synchronized keyword. Therefore, in certain scenarios, all StringBuffer objects are thread-safe, but in terms of execution efficiency, there will be a little loss due to the addition of thread synchronization operations;
In most cases, the effects of multi-threaded environments on the results are not required for String concatenation. Therefore, the StringBuilder type can improve the code execution efficiency.
When multiple threads share objects of the same StringBuffer type in code, you need to pay attention to the impact of the synchronized keyword on the final result. In the implementation of the StringBuffer class, only synchronized is used for each method. This ensures the consistency of the final result when accessing the same method of the StringBuffer object in the multi-thread scenario, if A thread accesses method A and method B, inconsistency may occur due to different lock objects. This is A special concern for programmers. For more information, see the implementation and application scenarios of Vector.


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.