10 basic java questions

1. What are the commonly used methods in the String class?

split(): split the string into a string array
indexOf(): extract the index position from the specified character
append(): append character or string
trim(): remove the spaces at both ends of the string
replace(): replace
hashCode(): Returns the hash code of the string
subString(): intercepts the string
equals(): compares
length(): obtains the length of the string
valueOf(): converts to a string
concat(): connects the specified string to this string end
compareTo (): used to compare two strings lexicographically
compareToIgnoreCase (): ignoring case lexicographically compare two strings
contains (): checks whether a string contains the value you want to find
a Which method in the string class replaces the fifth element?
String str = new String("helloWorld");
System.out.println(str.replace(5,'T'));

2. What is overloading and rewriting?

I understand that method overriding and overloading in Java refer to polymorphism in java. Overriding is the polymorphism between the parent class and the subclass. Overloading is a pair of polymorphism in a class The embodiment of  (1) Method overloading is defined in a class with multiple methods with the same name, and their number of parameters is different or the number is the same but the type and order are different, it is called method overloading (Overloading).
 (2) Method overriding is a method in which the method of the subclass has the same name as the method of the parent class, and the number of parameters is the same as the type, and the return value is the same method, which is called overriding.

3. The difference between HashMap and Hashtable

Both of these are implementation classes under the Map interface. When we develop, we often use HashMap. Although HashMap is not thread-safe, its storage efficiency is relatively high. Hashtable is thread-safe. We have seen his underlying put method before adding Synchronized keyword, but its efficiency is too low, we seldom use it in development. Why is hashTable thread-safe? Because when looking at the source code, the put method is preceded by the synchronized keyword, when to use hashMap? We have When customizing to return the json string, it is encapsulated in a List for return. Do you know the underlying implementation of Hashmap? Refer to the answer later

4. The underlying implementation principle of Hashmap

I have seen this on the forum. The bottom layer of Hashmap is realized through the combination of arrays and links. When we create a hashmap, we will create an array first. When we use the put method to store data, we first calculate the hash value according to the hashcode value of the key. Then use this hash value to determine the position in the array, and then put the value value in. If there is nothing in this position, it will be directly put in. If there is before, a linked list will be generated and the new value will be put in The value is placed in the head. When the value is obtained with the get method, the hash value is calculated according to the hashcode value of the key, the position is determined, and the value value is retrieved from the linked list at the position according to the equals method. Reference article
https://my.oschina.net/u/3954808/blog/3051948

5. How do hash collisions occur and how to solve them?

I happened to ask this question during the interview at my last home, and I didn’t know it at the time. I went back and checked it on the Internet. The solution is that there is an open address method when storing object addresses in the heap: when the address occurs. When there is a conflict, continue to detect other storage units in the hash table according to a certain method until an empty location is found.
The second is to use rehash (rehashing): When a conflict occurs, the second, third, and hash functions are used to calculate the address until there is no conflict. Disadvantage: Increased calculation time. For example, the first time the above is hashed according to the first letter of the surname, if there is a conflict, it can be hashed according to the second letter of the first letter of the surname, and then conflict, the third, until there is no conflict
3. Chain address method (zipper method): Create an array of linked lists. Each cell in the array is a linked list. If you encounter a hash conflict, add the conflicted value to the linked list. (HashMap in java is the method used)

6. The difference between ArrayList and LinkedList

ArrayList uses an array to store data, so querying data based on index is fast, and when adding or deleting elements, it is necessary to design a displacement operation, so it is slow.
LinkedList uses a two-way link to store data, and each element records the pointers of the elements before and after. Therefore, when inserting or deleting data, you only need to change the pointers of the elements before and after. The speed is very fast. Then you need to start indexing from the beginning when querying elements by subscript. So it is slower, but if you query the first few elements or the last few elements, the speed is faster. When is the ArrayList during development?, when we make queries, we often store the queried data in the arraylist.

7. What is the difference between IO and NIO?

This NIO is available after JDK1.7. The main difference between the two is: io is stream-oriented or blocking io, nio is buffer-oriented, non-blocking io; if
io is used, one or more bytes are read from the stream each time , Until all the bytes are read, it is not cached anywhere. What nio reads is that the data is cached, that is, the data he reads is read in the buffer. In addition, the various io in java are Blocking. That is to say, when a thread calls read or write(), the thread has been blocked until some data is read, or it is completely written. In this process, you can’t do other things. nio's non Blocking mode, when sending a request to read data, if the available data is not read, nothing will be obtained, and the thread will not be blocked for writing. The idle time of non-blocking IO can be used for Do other operations. Therefore, a single non-blocking thread can manage multiple input and output channels. In addition, NIO also has a selector (selector), which can manage multiple input and output channels. I probably understand that .

8. The state of the thread

In fact, threads generally have five states, namely, created, ready, running, blocked, and terminated.

Answer the following questions. For example, which method is to be called by you to start a thread?
New After a Thread object is created
, the thread is in the ready state after a new start(), and the thread changes after waiting for the CPU to call the CPU call. Become running:
After sleep is blocked, the lock on the monitor will not be released. After the thread sleeps, it becomes the ready state and the ready state
after yield. The lock will not be released.
After wait, other threads become blocked.
After nifity, other threads Become a ready state
suspend: is to change the thread suspension into a blocked state
resume: the suspended thread is restored and become the ready state
stop: stop the thread and become a dead state. The
run method runs out or becomes dead if an exception or break occurs. status

9.Thread synchronization, what is a lock?

There are three ways to achieve thread synchronization:
1) Synchronized code block: Add the "synchronized" keyword to the code block, then this code block is called a synchronized code block.
Synchronization code block format:
synchronized (monitoring object) { code that needs to be synchronized; } Explanation: There are three types of monitoring objects: object, String, .class file (as long as it is a constant object can be a monitoring object) 2) Synchronization method Synchronization method Definition format: synchronized method return value method name (parameter list) {}





Adding synchronized to the method is to use the current object as a monitor
3) Synchronization lock
Lock lock = new ReentrantLock(); (you can directly new in the class)
lock.lock(); The middle code block is locked lock.unlock ();

10. Custom Annotation, custom annotation

I wrote custom annotations when I was learning Java in the junior college. According to the following code, I haven't written them after development. They are all the annotations provided by the framework. The annotations of spring are explained one by one
and set in Annotation. Parameter content
Annotation definition format:
[public] @interface Annotation name { data type variable name (); } definition annotation: public @interface Controller {



}
Use annotation:
@Controller
public class UserController {

}
Define annotations with parameters (see api for details, you can write a demo if you have time).
Define annotations:
public @interface Controller { public String value(); // Define parameters } Use annotations: //@Controller(value=“userService” ) @Controller(“userService”) // When the parameter name of the annotation is value, the parameter name can be omitted public class UserController { } Common java built-in annotations, override etc.







Guess you like

Origin blog.csdn.net/zhang_yuanbai/article/details/108701777