Interview questions 30-day check-in-day05

1. What are the hashCode and equals methods in Java? How are they different from ==?

In Java, the hashCode and equals methods are methods of the Object class. Their functions are as follows:

  • The hashCode method returns the hash code of the object, which is used to support collections based on hash tables, such as HashMap, HashSet, etc. If the equals method of two objects returns true, then their hashCode method must return the same value, but not vice versa.
  • The equals method is used to compare objects for equality. By default, the equals method uses the == operator, which returns true only when two object references point to the same object. However, in most cases, we need to override the equals method to implement the equality rules defined by ourselves.

The difference between the two is that the hashCode method returns a value of type int, while the equals method returns a value of type boolean.

The hashCode method is used to quickly compare whether two objects are different, because if their hash codes are different, then they are definitely not equal. The equals method is used to judge whether two objects are really equal. This judgment is more complicated and needs to be defined according to the actual situation of the object.

In addition, it should be noted that the == operator is used to compare the reference equality of two objects, that is, whether they point to the same object. The equals method is used to compare the values ​​of two objects are equal.

In Java, object value comparison often needs to be defined according to the actual situation of the object, so it is generally necessary to rewrite the equals method.

My answer

The hashCode() method gets the hash value of the object.

equals() is used to compare values ​​for equality.

The difference between equals and ==:

  • When both sides of == are basic data types, it is comparing whether the values ​​are equal
  • When both sides of == are object (reference) types, it is to compare whether the object addresses in the heap pointed to by the two objects are the same, which is generally called comparing memory addresses.
  • The equals() method cannot be used to judge variables of basic types, but can only be used to judge whether two objects are equal. The bottom layer uses == to compare two objects, and returns a Boolean value.

The difference between hashCode and equals:

The two methods hashCode() and equals() are used to compare whether two objects are equal.

  • It is more efficient to use hashCode for comparison.
  • If the values ​​of equals of two objects are equal, then the hashCode values ​​are also equal.
  • When rewriting the equals() method, the hashCode() method must be rewritten. When rewriting the equals() method, if the hashCode() method is not rewritten, it may cause the equals() method to judge that the hashCode values ​​of the two objects are not equal.

2. What are the two core concepts of Spring? Briefly explain your understanding of them

The two cores of Spring are IoC (Inversion of Control, inversion of control) and AOP (Aspect Oriented Programming, aspect-oriented programming)

IoC: That is, inversion of control, or DI (Dependency Injectio) dependency injection. When we need an object, we no longer create it inside the class, but get it through the Spring container.

AOP: Aspect-oriented programming, which is used to extract and encapsulate public behaviors and logic that have nothing to do with business but affect multiple objects into a reusable module. Duplicated code reduces the coupling between modules and improves the maintainability of the system. Can be used for authority authentication, logging, transaction processing, etc.

3. What is deadlock? How to prevent and avoid deadlock?

Deadlock: During the execution process, two or more processes wait for each other due to competition for resources. If there is no external interference, they will be in a deadlock state.

Causes of deadlock:

  • Mutual exclusion: A resource can only be used by one process at a time.
  • Request and hold conditions: When a process is blocked due to a resource request, it holds the acquired resource.
  • Inalienable condition: The resource obtained through the process cannot be forcibly taken away until it is exhausted.
  • Circular waiting condition: A circular waiting resource relationship is formed between multiple processes.

Preventing and Avoiding Deadlocks

  1. Apply for all resources at once.
  2. break the mutex
  3. break the inalienable condition
  4. break request and hold condition
  5. Break out of circular wait condition

4. What are BOM and DOM? List some of their functions

DOM (Document Object Model) Document Object Model: Through DOM, you can modify the contents of the web page arbitrarily

  • Query element nodes by id: document.getElementById("id attribute value");
  • Query a set of element node objects according to the element's name and class name value: document.getElementsByName("name attribute value");
  • Query a set of element node objects according to the tag name: document.getElementsByTagName("tag name");
  • Find HTML elements by CSS selector: querySelectorAll()

BOM: (Browser Object Mode) browser object model, BOM operates the browser through JS, and provides a set of objects in the BOM to complete the operation of the browser. The BOM object allows JavaScript to talk to the browser

  • window.innerHeight The inner height of the browser window (including scroll bars)
  • window.innerWidth The inner width of the browser window (including scroll bars)
  • window.open() opens a new window
  • window.close() closes the window
  • window.moveTo() Move the current window
  • window.load() triggers this event when the document content is loaded, the old way of registering events

5. Tell me about your understanding of Node.js? Advantages and disadvantages? Application scenario?

Node.js is a JavaScript runtime environment based on the Chrome V8 engine, which allows developers to use JavaScript for server-side programming. Node.js has the characteristics of event-driven, non-blocking I/O, and can handle highly concurrent requests, so it is widely used in the development of real-time applications, Web applications, and APIs.

Following are the pros and cons of Node.js:

advantage:

  • Event-driven and non-blocking I/O features can handle highly concurrent requests, improving program performance and response speed;
  • Use JavaScript for development, with rich open source modules and components, which can greatly improve development efficiency;
  • Support cross-platform, can run on multiple operating systems such as Windows, Linux, MacOS;
  • Rapid prototyping and real-time debugging are possible.

shortcoming:

  • Node.js is not good enough for computing-intensive tasks and multi-threaded programming support, suitable for I/O-intensive tasks;
  • Because Node.js is based on event-driven and callback mechanisms, special attention should be paid to callback hell and exception handling of asynchronous operations during development, otherwise the code will be difficult to maintain.

The application scenarios of Node.js mainly include the following aspects:

  • Web development: use Node.js to quickly build a web server for web development;
  • Real-time application: Node.js supports event-driven and non-blocking I/O, which can be used in real-time data transmission and message communication and other fields;
  • Command-line tools: Node.js can be used to write command-line tools and scripts;
  • Microservices: Node.js supports cross-platform and lightweight development, and can be used to write microservices.

In short, Node.js has many advantages, such as high concurrent processing capability, cross-platform, rich open source components, etc., but it also needs to pay attention to its limitations. Developers need to choose technologies reasonably according to specific needs in practice.

Guess you like

Origin blog.csdn.net/qq_56098191/article/details/130178844