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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.
