Java common classic interview questions (1)

Java common classic interview questions (1)

1. Talk about the understanding of object-oriented?
Answer: In my understanding, object-oriented is a natural extension to the real world model, which is a programming idea of ​​"everything is an object". Any object in real life can be classified into a class of things, and each individual is an instance of a class of things. Object-oriented programming is object-centric and message-driven, so program=object+message.

When it comes to object-oriented, I have to mention its three major characteristics, encapsulation, inheritance, and polymorphism.
Encapsulation is to abstract the properties and behaviors of a class of things into a class, privatize the properties and make the behavior public, improve the privacy of the data, and make the code modular. Doing so makes the code more reusable.
Inheritance is to further abstract the properties and behaviors shared by a class of things into a parent class, and each subclass is a special parent class-with the behaviors and properties of the parent class, and also has its own unique behaviors and properties. This expands the existing code block and further improves the reusability of the code.
If encapsulation and inheritance are for code reuse, polymorphism is for interface reuse. Simply put, polymorphism is to allow parent class references (or interfaces) to point to subclass (or implementation class) objects.

2. What is the difference between Java and other languages?
Answer: This has to start with the characteristics of the java language.
1. Compile in one place and run in multiple places (the Java virtual machine is platform-independent).
2. Built-in memory management mechanism.
3. Support multithreading (C++ language does not have a built-in multithreading mechanism, so the multithreading function of the operating system must be invoked for multithreading programming).
4. It is an interpreted language (C language is a compiled language, there is the concept of pointer in C, and java cancels the concept of pointer)

3. What is the difference between JRE, JDK, and JVM?
Answer: First look at their respective concepts:
JVM: Java Virtual Machine, is a virtual machine that runs Java bytecode. JVM has specific implementations for different systems (Windows, Linux, macOS), but they all give the same results if they use the same bytecode.
JRE: Operating environment. It is a collection of everything needed to run a compiled Java program, including Java Virtual Machine (JVM), Java class libraries, java commands, and some other basic components.
JDK: It is a full-featured Java SDK. It has everything that JRE has, as well as a compiler (javac) and tools (such as javadoc and jdb). It can create and compile programs.
Simple understanding: JDK=JRE (including JVM + core class library (JavaAPI)) + development tools

4. What is bytecode? What are the benefits of using bytecode?
Answer: In Java, the code that JVM can understand is called bytecode (that is, a file with a .class extension). Any specific processor only faces the virtual machine; the
Java language uses bytecode to solve the problem of inefficient execution of the traditional interpreted language to a certain extent, while retaining the interpreted language Portable features

5. What is the role of configuration classpath?
Answer: This is the java system environment variable. We compile .java through the javac command to achieve, and the operation is implemented through java. These two commands are stored in the JDK package. If our .java file is not placed in the directory where the javac command is located, the system will report that javac cannot be found. The classpath is configured to be in any directory of the system. Can run javac java commands

6. Is the interface different before JDK1.8 and after JDK1.8?
Answer: Let’s first review what an interface is. The interface refers to the entrance of all services provided by the system (the actual interface defined by Interface, that is, the interface type) [can only have abstract methods]. It is an abstract type. A class inherits the abstract methods of the interface by implementing the interface; a class can only inherit one class, but can inherit multiple interfaces ; an interface cannot implement another interface, but can inherit multiple other interfaces
before JDK1.8:
1. The variables in the interface are all static variables (public static final) and must be explicitly initialized
2. All methods in the interface are public abstract by default
3. The interface has no construction method and cannot be instantiated, but it can be implemented (often Used as a type, that is, the parent class reference points to the subclass object)
4. The implementation class must implement all the methods of the interface
5. The implementation class can implement multiple interfaces (multiple inheritance in java)
After JDK1.8:
1. In the interface There can be a default method (method body) (modified by the default keyword). The
default method can be inherited, and there can be more than one called by an instance. If a class implements multiple interfaces and multiple interfaces define multiple same default methods, you need to implement the class to override the default method in the interface, otherwise an error will be reported. You can use super to call the default method of the specified interface.
2. Static methods
in theinterface that can be declared (and can be implemented) The static methods in theinterface must be public, the public modifier can be omitted, the static modifier cannot be omitted, and the static method cannot be inherited or covered, so it is only specific There can be multiple static methods in the interface.
To put it simply: before JDK1.8, the interface can only have abstract methods. After JDK1.8, there can be different methods in the interface, but the methods must be modified with static or default.

7. What is the difference between an interface and an abstract class?
Answer: The same point: both represent the abstract layer of the system; neither can be instantiated; both can contain abstract methods (used to describe the services provided by the system, without providing specific implementation).
Differences: a class can only inherit one direct parent class, However, multiple interfaces can be implemented;
abstract classes have constructors; interfaces have no constructors; abstract classes can have common methods in interfaces.
Whetherto distinguish between JDK versions;member variables in abstract classes have self-defined permissions to modify interface member variables by default as public static final;
from the design level, abstraction is an abstraction of classes, a kind of template design, and interface is an abstraction of what is done, and it is a norm of what is done

8. What are the differences between a Java application and a subprogram?
Answer: Simply put, the application is started from the main thread (that is, the main() method). The applet subprogram does not have a main() method, and it is mainly embedded in the browser screen to run (call init() or run() to start)

9. What are the commonly used containers in Java?
Answer: Data containers are mainly divided into two types, Collection: (storage of independent elements) and Map (storage of key-value type element pairs). The commonly used containers are:
ArrayList: Its data structure is a linear table. The advantage is that it is very convenient to access and query, but it is very inefficient when adding and deleting.
LinkedList: Its data structure uses a linked list. The advantage of this structure is that the efficiency of deletion and addition is very high, but the efficiency of random access to elements is lower than that of the ArrayList class.
HashSet: The Set class does not allow duplicate elements (sets), and cannot add a duplicate element (which already exists in the Set). HashSet uses the Hash function to optimize query efficiency, and its contain() method is often used to determine whether related elements have been added.
HashMap: provides a key-value key-value pair data storage mechanism, which can be very convenient Find the corresponding element through the key value, and through the Hash hash mechanism, it is very convenient to find.

10. What is the difference between thread and process?
Answer: What is a process?
The process is the execution process of the program, and the basic unit of the system to run the program, so the process is dynamic. The system running a program is the process of a process from creation, operation to death. In Java, when we start the main function, we actually start a JVM process, and the thread where the main function is located is a thread in this process, also known as the main thread.
What is a thread?
A thread is similar to a process, but a thread is a smaller unit of execution than a process. A process can spawn multiple threads during its execution. Different from a process, multiple threads of the same kind share the process’s heap and method area resources, but each thread has its own program counter, virtual machine stack, and local method stack, so the system is producing a thread, or When switching between threads, the burden is much smaller than the process. Because of this, threads are also called lightweight processes.
The difference between the two?
Address space: Threads of the same process share the address space of this process, and there are independent address spaces between processes.
Resource ownership: Threads in the same process share the resources of the process, such as memory, I/O, cpu, etc., but the resources between processes are independent.
Execution process: Each independent process has a program entry, sequential execution sequence, and program entry. However, threads cannot be executed independently, they must be stored in the application program, and the application program provides multiple thread execution control.
Thread is the basic unit of processor scheduling, but process is the basic unit of system operation program.

Guess you like

Origin blog.csdn.net/Z3068236782/article/details/114703047