JAVA interview manual

Posted by komquat on Sun, 05 Dec 2021 19:45:50 +0100

JAVA interview manual

JAVA overview

1. What is programming

Programming is a process in which a computer uses a programming language to write program code to solve a problem and finally get the result. In order to make the computer understand people's intention, human beings must tell the computer the ideas, methods and means of the problems to be solved in the form that the computer can understand, so that the computer can work step by step according to people's instructions and complete a specific task. This process of communication between people and computers is programming.

2. What is Java

Java Is an object-oriented programming language, which not only absorbs C++The advantages of language are also abandoned C++It is difficult to understand the concepts of multiple inheritance and pointers in Java Language has two characteristics: powerful function and easy to use. Java As the representative of static object-oriented programming language, language perfectly realizes the object-oriented theory and allows programmers to program complex programs in an elegant way of thinking.

Three major versions after 3.jdk1.5

Java SE(J2SE,Java 2 Platform Standard Edition,Standard Edition) Java SE Formerly known as J2SE. It allows the development and deployment of software for use on desktops, servers, embedded environments, and real-time environments Java Application. Java SE Includes support JavaWeb Service development class, and Java EE and Java ME Provide the foundation.

Java EE(J2EE,Java 2 Platform Enterprise Edition,Enterprise Edition) Java EE Formerly known as J2EE. The enterprise edition helps develop and deploy a portable, robust, scalable, and secure server side Java Application. Java EE Yes Java SE Based on, it provides Web Services, component models, management and communication API,It can be used to implement enterprise level service-oriented architecture( service-oriented architecture,SOA)and Web2.0 Application. February 2018, Eclipse Officially announced JavaEE Renamed JakartaEE

Java ME(J2ME,Java 2 Platform Micro Edition,Mini version) Java ME Formerly known as J2ME. Java ME For mobile devices and embedded devices (such as mobile phones PDA,Applications running on TV set-top boxes and printers) provide a robust and flexible environment. Java ME It includes flexible user interface, robust security model, many built-in network protocols and rich support for networked and offline applications that can be downloaded dynamically. be based on Java ME Standard applications can be written once for many devices, and can take advantage of the native functions of each device.

4. Relationship between JVM * *, JRE and JDK**

JVM
Java Virtual Machine yes Java Virtual machines, Java Programs need to run on virtual machines. Different platforms have their own virtual machines, so Java Language can realize cross platform.

JRE
Java Runtime Environment include Java Virtual machines and Java The core class library required by the program, etc. The core class libraries are mainly java.lang Package: contains run Java System classes that are essential to the program, such as basic data types, basic mathematical functions, string processing, threads, exception handling classes, etc. the system loads this package by default. If you want to run a developed package Java Program, only need to be installed in the computer JRE Just.

JDK
Java Development Kit Is provided to Java Used by developers, including Java Development tools, including JRE. So I installed JDK,There is no need to install it separately JRE Yes. Development tools: compilation tools(javac.exe),Packaging tools(jar.exe)etc.

5. What is cross platform? What is the principle

Cross platform refers to java The program written in language can run on multiple system platforms after one compilation. Implementation principle: Java The program is through java The virtual machine runs on the system platform, as long as the system can install the corresponding java Virtual machine, the system can run java Procedure.

6. What are the characteristics of Java language

Easy to learn( Java Grammar and of language C Language and C++Language is very close)

Object oriented (encapsulation, inheritance, polymorphism)

Platform independence( Java Virtual machine implementation (platform independence)

It supports network programming and is very convenient( Java Language was born to simplify network programming)

Support multithreading (multithreading mechanism enables applications to execute multiple tasks in parallel at the same time)

Robustness( Java Strong typing mechanism of language, exception handling, automatic garbage collection, etc.)

Security

7. What are the features of object orientation and your understanding of these features

1)Inheritance: inheritance is the process of obtaining inheritance information from an existing class and creating a new class. Classes that provide inheritance information are called parent classes (superclasses and base classes); The class that gets inheritance information is called a subclass (derived class). Inheritance makes the changing software system have a certain continuity. At the same time, inheritance is also an important means to encapsulate the variable factors in the program.

2) Encapsulation: encapsulation is generally considered to bind data and methods of operating data, and access to data can only be through defined interfaces. The essence of object-oriented is to describe the real world as a series of completely autonomous and closed objects. The method we write in the class is a kind of encapsulation of implementation details; We write a class that encapsulates data and data operations. It can be said that encapsulation is to hide everything that can be hidden and only provide the simplest programming interface to the outside world.

3) Polymorphism: polymorphism means that objects of different subtypes are allowed to respond differently to the same message. To put it simply, you call the same method with the same object reference, but do different things. Polymorphism is divided into compile time polymorphism and run-time polymorphism. If an object's method is regarded as a service provided by the object to the outside world, the runtime polymorphism can be interpreted as:
When A System access B When the system provides services, B The system has many ways to provide services, but everything is right A The system is transparent.
Method overloading( overload)It implements compile time polymorphism (also known as pre binding), and method rewriting( override)It implements runtime polymorphism (also known as post binding). Runtime state is the essence of object-oriented. To realize polymorphism, two things need to be done:
1. Method override (the subclass inherits the parent class and overrides the existing or abstract methods in the parent class);
2. Object modeling (using a parent type reference to reference a subtype object, so that the same reference calls the same method will show different behavior according to different subclass objects).

4)Abstraction: abstraction is the process of summarizing the common characteristics of a class of objects to construct a class, including data abstraction and behavior abstraction. Abstraction only focuses on the attributes and behaviors of objects, not the details of these behaviors.

Note: by default, object-oriented has three features: encapsulation, inheritance and polymorphism. If the interviewer asks to name four features, we will add abstraction.

8. Differences between the access rights modifiers public, private, protected, and not writing (default)

9. How to understand clone objects

In the actual programming process, we often encounter this situation: there is an object A,At some point A There are already some valid values in the. You may need a and A Identical new object B,And after that B Any changes will not affect A The value in, that is, A And B Are two separate objects, but B The initial value of is determined by A Object determined. stay Java In language, simple assignment statements can not meet this demand. Although there are many ways to meet this demand, it can be realized clone()Method is the simplest and most efficient means.

10. The difference between the process of creating a new object and the process of creating a clone object

new The original intention of the operator is to allocate memory. Program execution to new Operator, first look new Operator, because you know the type, you can know how much memory space to allocate. After allocating memory, call the constructor to fill in each field of the object. This step is called object initialization. After the construction method returns, an object is created, and its reference (address) can be published to the outside. The object can be manipulated externally with this reference.

clone The first step is and new Similarly, both allocate memory and call clone Method, the allocated memory and the original object (that is, the clone Method, and then use the corresponding fields in the original object to fill the fields of the new object. After filling, clone Method returns, a new same object is created, and the reference of the new object can also be published to the outside.

11. Use of clone object

The difference between copying objects and copying references

1. Person p = new Person(23, "zhang");
2. Person p1 = p;
3. System.out.println(p);
4. System.out.println(p1);
When Person p1 = p;After execution, is a new object created? First look at the print results:
1.com.itheima.Person@2f9ee1ac
2.com.itheima.Person@2f9ee1ac

It can be seen that the printed address values are the same. Since the addresses are the same, it must be the same object. p and p1 are just references. They both point to the same object Person(23, "zhang"). This phenomenon can be called replication of references. After the above code is executed, the scenario in memory is shown in the following figure:

The following code actually clones an object.

1.Person p = new Person(23, "zhang");
2.Person p1 = (Person) p.clone();
3.System.out.println(p);
4.System.out.println(p1);

It can be seen from the print results that the addresses of the two objects are different, that is, a new object is created instead of assigning the address of the original object to a new reference variable:

1. com.itheima.Person@2f9ee1ac
2. com.itheima.Person@67f1fba0

After the above code is executed, the scenario in memory is shown in the following figure:

12. Deep and light copies

In the following example code, there are two member variables in Person, name and age. Name is of type String and age is of type int. The code is very simple, as follows:

1.public class Person implements Cloneable{
2.privatint age ;
3. private String name;
4. public Person(int age, String name) {
5. this.age = age;
6. this.name = name;
7. } 8. public Person() {}
9. public int getAge() {
10. return age;
11. }
12. public String getName() {
13. return name;
14. }
15. @Override
16. protected Object clone() throws CloneNotSupportedException {
17. return (Person)super.clone();
18. }
19.}

Since age is a basic data type, there is no doubt about its copy. Just copy a 4-byte integer value. However, name is a String type. It is only a reference and points to a real String object. There are two ways to copy it: directly copy the reference value of name in the original object to the name field of the new object, or create a new same String object according to the String object pointed to by name in the original Person object, Assign the reference of the new String object to the name field of the newly copied Person object. These two copying methods are called shallow copy and deep copy respectively. The principle of deep copy and shallow copy is shown in the figure below:

The following is verified by code. If the address values of the names of the two Person objects are the same, it means that the names of the two objects point to the same String object, that is, shallow copy. If the address values of the names of the two objects are different, it means that they point to different String objects, that is, when copying the Person object, the String object referenced by name is copied at the same time, That is, deep copy. The verification code is as follows:

1. Person p = new Person(23, "zhang");
2. Person p1 = (Person) p.clone();
3. String result = p.getName() == p1.getName()
4. ? "clone It's a light copy" : "clone It's a deep copy";
5. System.out.println(result);

The print result is:

6. clone It's a light copy

Therefore, the clone method performs a shallow copy, which should be paid attention to when writing the program

How to make a deep copy:

From the contents of the previous section, we can draw the following conclusion: if you want to deeply copy an object, the object must implement the clonable interface and the clone method, and within the clone method, the other objects referenced by the object must also implement the clonable interface and the clone method. Then, according to the above conclusion, the following code is implemented. The Body class combines the Head class. In order to deeply copy the Body class, you must also copy the Head class in the clone method of the Body class. The code is as follows:

1.static class Body implements Cloneable{
2. public Head head;
3. public Body() {}
4. public Body(Head head) {this.head = head;}
5. @Override
6. protected Object clone() throws CloneNotSupportedException {
7. Body newBody = (Body) super.clone();
8. newBody.head = (Head) head.clone();
9. return newBody;
10. }
11.}
12.static class Head implements Cloneable{
13. public Face face;
14. public Head() {}
15. @Override
16. protected Object clone() throws CloneNotSupportedException {
17. return super.clone();
18. } }
19.public static void main(String[] args) throws CloneNotSupportedException {
20. Body body = new Body(new Head(new Face()));
21. Body body1 = (Body) body.clone();
22. System.out.println("body == body1 : " + (body == body1) );
23. System.out.println("body.head == body1.head : " + (body.head == body1.head));
24.}

The print result is:

1. body == body1 : false
2. body.head == body1.head : false

ntln("body == body1 : " + (body == body1) );
23. System.out.println("body.head == body1.head : " + (body.head == body1.head));
24.}

The print result is:

  1. body == body1 : false
  2. body.head == body1.head : false

Topics: Java JavaEE Interview