[Java Interview Questions] Summary of Common Java Interview Knowledge Points

Here are some of the more important Java interview questions, and the answers are confused, and explanations are added for easy understanding.

1. What is the Java virtual machine? Why is Java called a "platform-independent programming 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 is the difference between JDK and JRE?

The Java Development Kit (JDK) is a complete Java software development kit, including JRE, compiler and other tools (such as: JavaDoc, Java debugger), allowing developers to develop, compile, and execute Java applications.

Java Runtime Environment (JRE). It includes Java virtual machine, Java core class library and supporting files. It does not include development tools (JDK), compilers, debuggers and other tools.

3. What does the "static" keyword mean? Is it possible to override a private or static method in Java?

The "static" keyword indicates that a member variable or member method can be accessed without an instance variable of the class it belongs to. Static methods in Java cannot be overridden, because method overriding is based on dynamic binding at runtime, while static methods are statically bound at compile time. The static method is not related to any instance of the class, so it is conceptually not applicable.

Private methods cannot be overwritten in Java, because private modified variables and methods can only be used in the current class. If other classes inherit from the current class, private variables or methods cannot be accessed, and of course they cannot be overwritten.

If you need more interview materials from major companies, you can also click to enter directly and get it for free! Password: CSDN

4. Is it possible to access non-static variables in the static environment?

A static variable belongs to a class in Java, and its value is the same in all instances. When the class is loaded by the Java virtual machine, the static variable is initialized. If your code tries to access non-static variables without an instance, the compiler will report an error because these variables have not been created yet and have not been associated with any instances.

5. What are the data types supported by Java? What is automatic unpacking?

Java supports two data types: one is the basic data type, including byte, char, short, boolean, int, long, float, double; the other is the reference type: such as String, which is actually a reference to an object. The address of the object is stored in the virtual stack in the JVM, and the created object is essentially in the heap. The process of finding the object in the heap through the address is a reference type.

Automatic boxing is the conversion between the basic data types and the corresponding object packaging types by the Java compiler, that is, int is converted to Integer, and automatic unboxing is the process by which Integer calls its method to convert it to int.

6. What is the meaning of method overriding and method overloading in Java?

Method overloading in Java occurs when two or more methods in the same class have the same method name but different parameters. Override Override is a manifestation of polymorphism in a class.

Method coverage means that the subclass redefines the method of the parent class. Method coverage must have the same method name, parameter list, and return type. The overrider may not restrict access to the methods it covers. In Java, the subclass can inherit the method of the parent class, so there is no need to rewrite the same method. But sometimes the subclass does not want to inherit the method of the parent class intact, but wants to make certain modifications, which uses method rewriting. Method rewriting is also called method coverage.

7. What is a construction method in Java? What is constructor overloading? What is the copy construction method?

When the new object is created, the constructor is called. Every class has a constructor. In the case that the programmer does not provide a constructor for the class, the Java compiler will create a default constructor for this class.

Constructor overloading and method overloading in Java are very similar. You can create multiple constructors for a class. Each constructor must have its own unique parameter list.

Java does not support copy construction methods. If you do not write the construction method yourself, Java will not create a default copy construction method.

8. Does Java support multiple inheritance?

Classes in Java do not support multiple inheritance, only single inheritance (that is, a class has only one parent class). But the interface in java supports multiple inheritance, that is, a child interface can have multiple parent interfaces. (The function of the interface is to extend the function of the object. A sub-interface inherits multiple parent interfaces, indicating that the sub-interface extends multiple functions. When the class implements the interface, the class extends the corresponding function).

9. What is passing by value and passing by reference?

It is generally believed that the transfer in java is by value, and the transfer of instance objects in java is by reference.

Value transfer is for basic variables. What is transferred is a copy of the variable, and changing the copy does not affect the original variable;
reference transfer is generally for object variables, passing a copy of the object address, and Not the original object itself.

10. In the monitor (Monitor), how is thread synchronization done? What level of synchronization should the program do?

The monitor and the lock are used together in the Java virtual machine. The monitor monitors a block of synchronized code to ensure thatOnly one thread executes synchronized code blocks. Each monitor is associated with an object reference. The thread is not allowed to execute synchronization code before acquiring the lock.

11. What is a deadlock (deadlock)?

The so-called deadlock refers to multiple process causesCompeting resourcesAnd the resulting stalemate (waiting for each other), without external force, these processes will not be able to move forward. Four necessary conditions for deadlock:
Mutually exclusive conditions: The process requires exclusive control of the allocated resources (such as printers), that is, a certain resource is only occupied by one process for a period of time. At this time, if another process requests the resource, the requesting process can only wait.
Non-deprivation conditions: the resources acquired by the process cannot be forcibly taken away by other processes before they are used up, that is, the process that obtains the resource can only release it by itself (only active release).
Request and retention conditions: The process has retained at least one resource, but has made a new resource request, and the resource has been occupied by other processes. At this time, the requesting process is blocked, but it retains the resources it has acquired.
Loop waiting condition: there is a kind of process resourceCyclic waiting chain, The resources obtained by each process in the chain are requested by the next process in the chain at the same time.

12. How to ensure that N threads can access N resources without causing deadlock?

When using multithreading, a very simple way to avoid deadlock is:Specify the order of acquiring locks, And force threads to acquire locks in the specified order. Therefore, if all threads add and release locks in the same order, there will be no deadlock.

13. What are the basic interfaces of the Java collection framework?

The collection interface specifies a set of objects called elements. Each specific implementation class of the collection class interface can choose to store and sort the elements in its own way. Some collection classes allow duplicate keys, some do not.
Java collection classes provide a set of well-designed interfaces and classes that support operations on a set of objects. The most basic interfaces in the Java collection class are:
Collection: Represents a group of objects, each of which is its child element.
Set: A Collection that does not contain repeated elements.
List: An ordered collection, and can contain repeated elements.
Map: An object that can map a key to a value, and the key cannot be repeated.

14. Why does the collection class not implement the Cloneable and Serializable interfaces?

The semantics and meaning of cloning or serialization are related to specific implementations. Therefore, the specific implementation of the collection class should decide how to be cloned or serialized.

15. What is iterator (Iterator)?

The Iterator interface provides many methods for iterating over collection elements. Each collection class contains
iteration methods that can return an iterator instance . The iterator can delete the elements of the underlying collection in the iterative process, but it cannot directly call the
remove(Object Obj) of the collection to delete it. It can be deleted through the remove() method of the iterator.

Reader benefits

Thank you for seeing here!
I have compiled a lot of 2021 latest Java interview questions (including answers) and Java study notes here, as shown below
Insert picture description here

The answers to the above interview questions are organized into document notes. As well as interviews also compiled some information on some of the manufacturers & interview Zhenti latest 2021 collection (both documenting a small portion of the screenshot) free for everyone to share, in need can click to enter signal: CSDN! Free to share~

If you like this article, please forward it and like it.

Remember to follow me!
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49527334/article/details/114901506