Summary of java backend interview questions

This article will be continuously updated. . . . . Summary of interview questions

    During the interview, when a leader asks you why you are leaving, you must think carefully about how to answer it. If you don’t know, you can Baidu it. I am still in the groping stage. If you have an idea, you can leave a message for discussion. This question is very important!

1.  What does RESTful mean?

    One of the most popular Internet software architectures at present, resources transfer state in a certain form in the network (1) each URI represents a resource; (2) between the client and the server, some kind of transfer of this resource is carried out. Presentation layer; (3) The client operates server-side resources through four HTTP verbs to achieve "presentation layer state transformation". (It feels very obscure, I don't understand very well)

2. What methods are in the Object class, please give a few examples.

    clone(), equals(), hashCode(), toString(), notify(), notifyAll(), wait(), finalize(), getClass()

3. What is the difference between StringBuilder and StringBuffer?

   StringBuilder is thread-unsafe, while StringBuffer is thread-safe, so in terms of speed (which can also be understood as performance) StringBuilder > StringBuffer > String. String is immutable, the other two are mutable. Many methods of StringBuffer are modified by synchronized, so it is thread-safe, but it will slow down.

4. What is the difference between List and Set?

    List, Set are inherited from the Collection interface. List elements are ordered, set elements are unordered, and repeated elements will overwrite the previous elements.

5. What are the ways of Set traversal?

    (1)Set<String> sets = new HashSet<String>();for (String element : sets) {}   (2)Set<String> sets = new HashSet<String>();Iterator<String> it = sets.iterator();while(it.hasNext()){String str = it.next()}

6. What are the ways of Map traversal?

    Map<String, Integer> tempMap = new HashMap<String, Integer>();  (1)Iterator it = tempMap.entrySet().iterator();  while (it.hasNext()) {  Map.Entry entry = (Map.Entry) it.next();  Object key = entry.getKey();   Object value = entry.getValue();  }(2)for (Map.Entry<String, Integer> entry : tempMap.entrySet()) {String key = entry.getKey().toString();String value = entry.getValue().toString();  }(3) for (Object o : tempMap.keySet()) {  System.out.println("key=" + o + " value=" + tempMap.get(o));  }(4)for (Iterator i = tempMap.keySet().iterator(); i.hasNext();) {   Object obj = i.next();   System.out.println(obj);// 循环输出key   System.out.println("key=" + obj + " value=" + tempMap.get(obj));    }

    Here mainly remember entrySet and keySet, use iterator iterator.

7. What is the reflection mechanism of java?

    Get the properties and methods of the class in the Java runtime environment, and call any method of the object. The dynamic acquisition of class information and the method of dynamically invoking objects is called Java's reflection mechanism.

8. What are the characteristics of transactions, please explain a little?

    ACID, Atomicity: Transactions cannot be split, either all or none. Consistency: A transaction must cause the database to transition from one consistent state to another. Isolation: When multiple users access, the transactions between users do not interfere with each other. Durability: Once the transaction is committed, the data changes in the database are permanent.

8. How to get an object

    A.class a.getclass() Class.forName()

9. What is ThreadLocal used for?

    ThreadLocal (copy of thread variable), ThreadLocal maintains a local variable for each thread. While Synchronized implements memory sharing, ThreadLocal uses space for time, which is used for data isolation between threads, providing a copy for each thread that uses the variable, and each thread can change its own copy independently, without other Thread's copy conflict.

10. What is the difference between & and &&?

    The & operator can be used in two ways: (1) bitwise AND; (2) logical AND. The & operator is a short-circuit AND operation. The difference between logical and and short-circuit and is very huge, although both require that the Boolean values ​​on the left and right ends of the operator are both true. The value of the entire expression is true. The reason why && is called a short-circuit operation is that if the value of the expression on the left of && is false, the expression on the right will be directly short-circuited and no operation will be performed. Many times we may need to use && instead of &.

11. Addition and subtraction of byte?

    The title is byte a=1, b=2, ab; final byte c=3, d=4, cd; ab=a+b; cd=c+d;

    syso(ab);syso(cd);

    The variables modified by final are constants, here cd=c+d can be regarded as cd=7; it has become cd=7 at compile time. And b1 and b2 are byte types, they are calculated in java Promote to int type, and then perform calculation. After b1+b2 is calculated, it is already int type, and it is assigned to b3. b3 is byte type. If the type does not match, the compilation will not pass, and a forced conversion is required. In Java, byte, short, and char are all promoted to int type when performing calculations.

12.  What is the difference between a thread and a process?

    For thread related information, you can visit the following address (https://www.cnblogs.com/bsjl/p/7693029.html). Threads are a subset of processes. A process can have many threads, and each thread executes different tasks in parallel. Different processes use different memory spaces, and all threads share the same memory space. Don't confuse it with stack memory, each thread has a separate stack memory for storing local data.

13. What is thread safety? Is Vector a thread safe class?

    If your code is in a process with multiple threads running at the same time, these threads may be running this code at the same time. If the result of each run is the same as the result of a single-threaded run, and the values ​​of other variables are the same as expected, it is thread-safe. The same instance of a thread-safe counter class will not fail miscalculation if it is used by multiple threads. Obviously you can divide collection classes into two groups, thread-safe and non-thread-safe. Vector is thread-safe using synchronized methods, while ArrayList like it is not thread-safe.

14. What is a thread pool? Why use it?

    Creating a thread takes expensive resources and time. If a thread is created before a task arrives, the response time will be longer, and a process can create a limited number of threads. In order to avoid these problems, several threads are created to respond to processing when the program starts. They are called thread pools, and the threads in them are called worker threads. Starting from JDK1.5, the Java API provides the Executor framework so that you can create different thread pools. For example, a single thread pool, which processes one task at a time; a fixed number of thread pools or a cached thread pool (a scalable thread pool suitable for programs with many short-lived tasks).

15. What is the difference between calling wait() and sleep() methods in Java multithreading?

    Both wait and sleep in Java programs cause some form of pause, and they can meet different needs. The wait() method is used for inter-thread communication. If the wait condition is true and other threads are woken up, it will release the lock, while the sleep() method only releases CPU resources or stops the current thread from executing for a period of time, but does not release the lock.

1 6. What is the difference between local code block, construction code block and static code block?

    First explain with code:

public class Main {
    public static void main(String[] args) {
        B b =new B();
    }
}
class A{
    {
        System.out.println("Construct block A");
    }
    static{
        System.out. println("Static block A");
    }
    public A(){
        System.out.println("Constructor A");
    }
}
class B extends A{
    {
        System.out.println("Constructor Block B") ;
    }
    static{
        System.out.println("Static block B");
    }
    public B(){
        System.out.println("Constructor B");
    }
}
    The output is:

        Static Code Block A
        Static Code Block B
        Constructor Code Block A
        Constructor A
        Constructor Code Block B
        Constructor B

Explanation: 1. Local code block, also known as ordinary code block. It is a block of code that acts on a method. Function: It controls the life cycle of local variables.
         2. To construct a code block is to initialize all objects. It will be executed every time an object is created, and the construction code is faster than the constructor execution. The difference between the construction code block and the constructor is: the construction code block is to initialize all objects uniformly, and the constructor is to initialize the corresponding object, because there can be multiple constructors, which constructor is run will establish what kind of object, but no matter which object is created, the same block of construction code is executed first. That is to say, what is defined in the construction code block is the initialization content common to different objects.
         3. Static code block is to initialize the class. If a class contains multiple static code blocks, it is executed according to the code execution order. Executed as classes are loaded, and only once. Take precedence over the main function execution. When a class comes into memory, static code loading occurs first

17. What are the ways to use threads?

         1. Inherit the Thread class.
         2. Implement the Runabl interface There is no big difference between the two. Java is a single inheritance, so it is generally better to write the second thread.

18. The difference between the run method and the start method of the calling thread

         1. The execution of the run method is not asynchronous, but synchronous. This thread object is not handed over to the thread planner for planning.
         2. The start method is the general way to start a thread and is executed asynchronously. The order in which start is executed does not represent the order in which threads are executed

19. The difference between thread interrupted and isInterrupted

         1. interrupted Test whether the current thread is already in the interrupted state, and clear the state flag to false after execution.
         2. isInterrupted Tests whether the current thread has been interrupted, but does not clear the status flag.

      

    

 

        

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325246022&siteId=291194637
Recommended