2021 School Recruitment Convinced Some Written Test Questions (Summary of the Most Complete Links to Knowledge Points)

Checking for omissions and filling in vacancies is a summary of other people's knowledge.

1. String & StringBuffer & StringBuilder

Problems creating objects

The original text of the following three sentences
(1) Now when someone asks String str = new String("abc"); to create several objects, there is one abc field in the constant pool, and two in the constant pool without the "abc" field .
(2) String str="abc"; Create several objects (0 if there are already objects in the constant pool, 1 if not); (3)
new String("abc").intern(); Several objects are created (one if the string object already exists in the constant pool, two if not)
String str = new String("abc") in Java creates several objects

StringBuilder 的 toString()

Note that a new new String object is returned

 @Override
    public String toString() {
    
    
        // Create a copy, don't share the array
        return new String(value, 0, count);
    }

intern() method

It's transparent!
The intern() method returns a canonical representation of a string object.

String.intern() is a Native method, its function is:
if the character constant pool already contains a string equal to this String object, return the reference of the string in the constant pool, otherwise, put the new string into Constant pool, and returns a reference to the new string object

The intern() method of String can be used to expand the constant pool.

2. String constant pool, Class constant pool, runtime constant pool

String constant pool, Class constant pool, runtime constant pool

3. ThreadLocal

Each Entry of ThreadLocalMap is a weak reference to the key, and each Entry contains a strong reference to the value.

1. There is a map in Thread, which is ThreadLocalMap

2. The key of ThreadLocalMap is ThreadLocal , and the value is set by ourselves.

3. ThreadLocal is a weak reference. When it is null, it will be garbage collected

4. The important point is that suddenly our ThreadLocal is null, that is, it will be recycled by the garbage collector, but at this time our ThreadLocalMap life cycle is the same as that of Thread, it will not be recycled, and a phenomenon appears at this time. That is, the key of ThreadLocalMap is gone, but the value is still there, which causes a memory leak.

Solution: After using ThreadLocal, execute the remove operation to avoid memory overflow.

The entire site is talking about ThreadLocal including a brief introduction to the four types of references.
(As an understanding, I feel that what he said is very deceptive, and some places are not accurate enough)

Interpretation of ThreadLocal in detail

Personally, I think this is the best

Function:
1. When transferring objects across layers, using ThreadLocal can avoid multiple transfers and break the constraints between layers.

2. Data isolation between threads, that is, it cannot be used for shared resources between threads, but can only be shared between methods of the same thread.

3. Perform transaction operations to store thread transaction information.

4. Database connection:

5. Session session management.

4. Atomic classes

Atomic update basic type
addAndGet(int delta) : atomically add the input value to the original value in the instance, and return the final result;
incrementAndGet(): atomically add 1 to the original value in the instance, and return the final added value Result;
getAndSet(int newValue): update the value in the instance to a new value, and return the old value;
getAndIncrement(): add 1 to the original value in the instance in an atomic way, and return the old value before self-increment;
the atomic class updates various data types

A good blog post about atomic classes

5. JVM thread sharing, thread isolation

In the JAVA virtual machine, there is a runtime data area, including
thread sharing : heap, method area
thread isolation : program counter, Java virtual machine stack, and local method stack.

6. The most recent number

Given nums[]. Return a new array arr[].
arr[i] satisfies x < i & nums[x] < nums[i] && y > i & nums[y] > nums[i] , arr[i ] = nums[y] - nums[x].
x , y is closest to i.

This is roughly the case.

7. Merge IPV4 addresses

Given an ArrayList, which stores the IP address. Contains a single IP address or a range of IP addresses.
If the IP addresses are consecutive, they are merged into one range. If the IP address is included in a certain IP range, it will be merged into this range, and
the ascending order of the IP addresses will be returned.

Guess you like

Origin blog.csdn.net/qq_43709785/article/details/120032645