Java basic interview questions (notes finishing)

Theoretical questions

1. What is a java virtual machine? Why is java called a "platform independent language"?
The Java virtual machine is a virtual machine process that executes bytecode files (.class). The java source program (.java) is compiled into a bytecode file (.class) by the compiler. Then the bytecode file will be interpreted into machine code by the java virtual machine (the machine code is different for different platforms). Use machine code to operate hardware and operating systems.
Because different platforms are equipped with different JVMs, they can interpret the same .class file into the machine code required by different platforms. It is precisely because of the existence of JVM that java is called a platform-independent programming language.

2. What data types does JAVA support? What is automatic unpacking?
1) 8 basic data types: byte short char boolean int long float double
2) Reference types, including
automatic unboxing of classes, interfaces, and arrays , refers to the automatic conversion between basic data types and reference data types, such as Integer and int can be converted automatically; Float and float can be converted automatically

3. Briefly describe the principle of parameter transfer in the method?
Value transfer: (formal parameter type is the basic data type): When the method is called, the actual parameter passes its value to the corresponding formal parameter. The formal parameter just initializes its own storage unit content with the value of the actual parameter. There are two different Storage unit, so the change of formal parameter value during method execution does not affect the actual parameter value.
Pass by reference: (formal parameter type is a reference data type parameter): also known as pass address. When the method is called, the actual parameter is an object (or array). At this time, the actual parameter and the formal parameter point to the same address. During the method execution, the operation on the formal parameter is actually the operation on the actual parameter. The result is after the method ends. It is retained, so changes in formal parameters during method execution will affect actual parameters.

4. Briefly describe the difference between abstract class and interface?
(1) Interfaces can only contain abstract methods, and abstract classes can contain ordinary methods.
(2) Interfaces can only define static constant attributes. Abstract classes can define common attributes as well as static constant attributes.
(3) The interface does not contain the constructor, and the abstract class can contain the constructor.
(4) An abstract class cannot be instantiated, but it does not mean that it cannot have a constructor. An abstract class can have a constructor, which is extended by inherited classes.
(5) The interface is the core, which defines the things to be done and contains many Methods, but does not define how these methods should be done.
(6) If many classes implement a certain interface, then each must use code to implement those methods
(7) If the realization of certain classes has something in common, an abstract class can be abstracted to allow the abstract class to implement the interface Common code, and those personalized methods can be implemented by each subclass.

5. What are the basic interfaces of the Java collection framework? And explain their basic characteristics.
The most basic interfaces in the Java collection class are:
Collection: Represents a group of objects, each of which is its child element.
Set: does not contain repeated elements.
List: An ordered collection, and can contain repeated elements, including arraylist and linkedlist.
Map: An object that can map a key to a value, and the key cannot be repeated.

6. What is the importance of finally code blocks in exception handling? (Hint: generally what is defined in the finally block)
No matter whether an exception is thrown or not, the finally code block will always be executed. Even if there is no catch statement and an exception is thrown at the same time, the finally code block will still be executed. The last thing to say is that the finally code block is mainly used to release resources, such as: I/O buffers, database connections.

7. What is generic and why should I use generic?
Generic: The essence is a parameterized type. The essence of generics is to parameterize types (in the case of not creating a new type, through the different types specified by generics to control the type of specific restrictions on formal parameters). That is to say, in the process of using generics, the data type of the operation is designated as a parameter. This parameter type can be used in classes, interfaces, and methods, which are called generic classes, generic interfaces, and generic methods, respectively.
① Switching to generics can increase readability and stability, and it is more convenient without forced conversion.
② Use generics to improve code reuse and become more modular.
③Type safety. Only the specified type can be added to the collection.

Guess you like

Origin blog.csdn.net/weixin_43935558/article/details/108609227