Home>Article>Java> The 8 most common mistakes in Java interview questions

The 8 most common mistakes in Java interview questions

(*-*)浩
(*-*)浩 Original
2019-11-11 14:21:36 1985browse

The 8 most common mistakes in Java interview questions

1. Usage of static and final

The role of static can be discussed from three aspects, namely static variables, static methods, static kind.

Static variables:Static variables declared as static are essentially global variables. When an object is declared, a copy of the static variable is not generated, but all instance variables of the class share the same A static variable. In other words, this static variable is only loaded once and only a piece of storage space is allocated.

Static methods:Static methods declared as static have the following characteristics:

(1) Static methods can only call static methods;

(2) Static methods can only access static data;

(3) Static methods cannot reference this or super in any way;

Static class:Usually an ordinary class Declaration as static is not allowed, only an inner class is allowed (the main method is a typical example). At this time, the declared static class can be used directly as a normal class without the need to instantiate an external class.

The role of final can be understood from three aspects: variables, methods, and classes:

The value of the variable modified by final cannot be modified and is a constant;

The value of the variable modified by final Methods cannot be overridden;

final-modified classes cannot be inherited;

2. The difference between abstract classes and interfaces. Can a class inherit multiple classes? Can an interface inherit multiple classes? An interface? Can a class implement multiple interfaces?

Neither abstract classes nor interfaces can be instantiated directly. If they are to be instantiated, abstract class variables must point to subclass objects that implement all abstract methods, and interface variables must point to class objects that implement all interface methods.

Abstract classes must be inherited by subclasses, and interfaces must be implemented by classes.

Interfaces can only make method declarations. Method declarations can be made in abstract classes, and method implementations can also be made.

The variables defined in the interface can only be public static constants. In abstract classes, Variables are ordinary variables.

All abstract methods in an abstract class must be implemented by the subclass. If the subclass cannot implement all the abstract methods of the parent class, then the subclass can only be an abstract class. Similarly, when a class implements an interface, if it cannot implement all interface methods, then the class can only be an abstract class.

Abstract methods can only be declared, not implemented. abstract void abc(); cannot be written as abstract void abc(){}.

There can be no abstract methods in abstract classes.

If there is an abstract method in a class, then the class can only be an abstract class.

Abstract methods must be implemented, so they cannot be static or private.

Interfaces can inherit interfaces and multiple interfaces, but classes can only inherit from a single root.

3. Functions and usage of this and super

#this:

(1) Can access except the constructor method All properties and methods are called through this.

(2) Cannot be used in static methods

(3) Use this (parameter list) in the constructor to call Other construction methods of this class must be placed in the first sentence of the construction method.

super:Access the methods and properties of the parent class

(1) Access the methods and properties of the parent class;

(2) In the constructor The constructor of the parent class is called through super (parameter list), which must be placed in the first line of the constructor of the subclass.

4. What is the difference between final, finally, finalize?

final: The modifier (keyword) has three uses: If a class is declared as final, it means It cannot derive new subclasses, that is, it cannot be inherited. Declaring variables as final ensures that they will not be changed during use. Variables declared as final can only be read and cannot be modified in references after initialization. Methods declared as final can also only be used and cannot be overridden in subclasses.

finally: Usually placed after try...catch, the structure always executes the code block, which means that no matter whether the program is executed normally or an exception occurs, the code here can be executed as long as the JVM is not closed, and the external code can be released The resource code is written in the finally block.

finalize: A method defined in the Object class. Java allows the use of the finalize() method to do necessary cleanup work before the garbage collector clears the object from memory. This method is called by the garbage collector when it destroys an object. By overriding the finalize() method, you can organize system resources or perform other cleanup work.

5. What is the difference between Error and Exception?

Error represents system-level errors and exceptions that the program does not need to handle. It is a situation where recovery is not impossible but difficult. A serious problem; such as memory overflow, it is impossible to expect the program to handle such a situation;

Exception represents an exception that needs to be caught or needs to be handled by the program, which is a design or implementation problem; that is to say , which represents a situation that would never occur if the program were running normally.

6. Tell the life cycle of Servlet and the difference between Servlet and CGI.

After the Servlet is instantiated by the server, the container runs its init method, and when the request arrives, it runs its service method. The service method automatically dispatches the doXXX method (doGet, doPost) corresponding to the request, etc., when the server decides to destroy the instance. Call its destroy() method.

The difference with CGI is that Servlet is in the server process. It runs its service method through multi-threading. One instance can serve multiple requests, and its instance is generally not destroyed, while CGI processes each request. Both generate new processes and destroy the service after it is completed, so the efficiency is lower than Servlet.

7. How to prevent cache avalanche?

Cause:

Cache avalanche may be because the data is not loaded into the cache, or the cache fails in a large area at the same time, causing all requests to go Query the database, causing the database CPU and memory load to be too high, or even downtime.

Corresponding solution:

Use lock counting, or use a reasonable number of queues to avoid causing too much pressure on the database when the cache fails. Although this method can relieve the pressure on the database, it also reduces the throughput of the system.

Analyze user behavior and try to distribute failure time points evenly. Avoid cache avalanches.

If a certain cache server is down, you can consider primary and backup, such as redis primary and backup. However, double caching involves update transactions, and update may read dirty data, which needs to be solved.

8. Talk about your understanding of MVC

MVC is the abbreviation of Model-View-Controler. That is model-view-controller. MVC is a design pattern that enforces separation of input, processing, and output of an application.

The models, views, and controllers in MVC are responsible for different tasks.

View: A view is the interface that users see and interact with. Views display relevant data to the user and accept input from the user. Views do not perform any business logic processing.

Model: Model represents business data and business processing, equivalent to JavaBean. A model can provide data to multiple views. This improves application reusability.

Controller: When the user clicks the submit button in the Web page, the controller accepts the request and calls the corresponding model to process the request, and then calls the corresponding view to display the processing results based on the processing results.

MVC processing process: First, the controller accepts the user's request, calls the corresponding model for business processing, and returns data to the controller. The controller calls the corresponding view to display the processing results. and presented to the user through views.

The above is the detailed content of The 8 most common mistakes in Java interview questions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn