Did the Java interview fail? Have you read this article? ——Java Basic Interview Questions (1)

1. The difference between object-oriented and process-oriented

Insert picture description here

Process-oriented

Advantages: The performance is higher than object-oriented, because the class calls need to be instantiated, the overhead is relatively large, and consumes more resources; for example, single-chip, embedded development, Linux/Unix, etc. generally adopt process-oriented development, and performance is the most important factor.

Disadvantages: No object-oriented, easy to maintain, easy to reuse, easy to expand

Object-oriented

Advantages: easy to maintain, easy to reuse, easy to expand, due to the object-oriented features of encapsulation, inheritance, and polymorphism, a low-coupling system can be designed, making the system more flexible and easier to maintain

Disadvantages: performance is lower than process-oriented

2. What are the characteristics of the Java language

  • Easy to learn;
  • Object-oriented (encapsulation, inheritance, polymorphism);
  • Platform independence (Java virtual machine realizes platform independence);
  • reliability;
  • safety;
  • Support for multi-threading (C++ language does not have a built-in multi-threading mechanism, so the multi-threading function of the operating system must be called for multi-threading programming, while Java language provides multi-threading support);
  • It supports network programming and is very convenient (the birth of the Java language itself is designed to simplify network programming, so the Java language not only supports network programming but also is very convenient);
  • Compilation and interpretation coexist;

3. The most detailed and popular answers about JVM JDK and JRE

JVM

The Java Virtual Machine (JVM) is a virtual machine that runs Java bytecode. JVM has specific implementations for different systems (Windows, Linux, macOS), the purpose is to use the same bytecode, they will all give the same results.

What is bytecode? What are the benefits of using bytecode?

In Java, the code that the JVM can understand is called bytecode (that is, a file with a .class extension), which does not face any specific processor, but only faces the virtual machine. The Java language solves the problem of low execution efficiency of traditional interpreted languages ​​to a certain extent through bytecode, while retaining the portability of interpreted languages. Therefore, the Java program is more efficient at runtime, and because the bytecode is not specific to a specific machine, the Java program can run on a variety of different computers without recompiling.

There are generally three steps from source code to running of a Java program:

**We need to pay special attention to the step of .class->machine code. **In this step, the jvm class loader first loads the bytecode file, and then interprets and executes it line by line through the interpreter. The execution speed of this method will be relatively slow. Moreover, some methods and code blocks are often called, which is the so-called hot code, so the JIT compiler was introduced later, and JIT belongs to runtime compilation. When the JIT compiler completes the first compilation, it will save the machine code corresponding to the bytecode, which can be used directly next time. And we know that the operating efficiency of machine code is definitely higher than that of the Java interpreter. This also explains why we often say that Java is a language where compilation and interpretation coexist.

HotSpot adopts the method of Lazy Evaluation. According to the law of 28, only a small part of the code (hot code) consumes most of the system resources, and this is the part that JIT needs to compile. The JVM collects information according to the execution of the code each time and makes some optimizations accordingly, so the more times it is executed, the faster it will be. JDK 9 introduced a new compilation mode AOT (Ahead of Time Compilation), which directly compiles bytecode into machine code, thus avoiding various overheads such as JIT preheating. JDK supports layered compilation and AOT collaborative use. However, the compilation quality of the AOT compiler is definitely not as good as the JIT compiler.
Summary: The Java Virtual Machine (JVM) is a virtual machine that runs Java bytecode. JVM has specific implementations for different systems (Windows, Linux, macOS), the purpose is to use the same bytecode, they will all give the same result. Bytecode and JVM implementation of different systems are the key to the Java language "compile once, run anywhere".

JDK 和 JRE

JDK is the Java Development Kit, which is a fully functional Java SDK. It has everything that JRE has, as well as compilers (javac) and tools (such as javadoc and jdb). It can create and compile programs.
JRE is the Java runtime environment. It is a collection of everything needed to run a compiled Java program, including the Java Virtual Machine (JVM), Java class libraries, java commands, and other basic components. However, it cannot be used to create new programs.
If you just want to run a Java program, then you only need to install JRE. If you need to do some Java programming work, then you need to install the JDK. However, this is not absolute. Sometimes, even if you do not plan to do any Java development on your computer, you still
need to install the JDK. For example, if you want to deploy a web application using JSP, then technically speaking, you are just running a Java program in the application server. So why do you need JDK? Because the application server will convert JSP to Java servlet, and need to use JDK to compile the servlet.
Insert picture description here

4. Comparison of Oracle JDK and OpenJDK

Maybe many people like me have not touched and used OpenJDK before looking at this issue. So is there a major difference between Oracle and OpenJDK? Below, I will use some information I collected to answer this question that is ignored by many people.

For Java 7, there is nothing critical. The OpenJDK project is mainly based on the HotSpot source code donated by Sun. In addition, OpenJDK was selected as the reference implementation of Java 7, maintained by Oracle engineers. Regarding the difference between JVM, JDK, JRE and OpenJDK, an Oracle blog post in 2012 has a more detailed answer:

Q: What is the difference between the source code in the OpenJDK repository and the code used to build the Oracle JDK?

Answer: Very close-our Oracle JDK version build process is based on OpenJDK 7, only a few parts are added, such as deployment code, which includes the implementation of Oracle's Java plug-in and Java WebStart, and some closed source code party components, such as Graphics rasterizer, some open source third-party components, such as Rhino, and some odds and ends, such as additional documents or third-party fonts. Looking to the future, our goal is to open source all parts of the Oracle JDK, except for the part where we consider commercial functions.

to sum up:

  1. The Oracle JDK version will be released every three years, and the OpenJDK version will be released every three months;
  2. OpenJDK is a reference model and is completely open source, while Oracle JDK is an implementation of OpenJDK, not completely open source;
  3. Oracle JDK is more stable than OpenJDK. The code of OpenJDK and Oracle JDK is almost the same, but Oracle JDK has more classes and some bug fixes. Therefore, if you want to develop enterprise/commercial software, I suggest you choose Oracle JDK because it has been thoroughly tested and stable. In some cases, some people mentioned that they may encounter many application crashes when using OpenJDK, but just switch to Oracle JDK to solve the problem;
  4. Top companies are using Oracle JDK, such as Android Studio, Minecraft and IntelliJ IDEA development tools, of which Open JDK is not very popular;
  5. In terms of responsiveness and JVM performance, Oracle JDK provides better performance than OpenJDK;
  6. Oracle JDK will not provide long-term support for the upcoming version. Users must obtain the latest version by updating to the latest version to obtain support each time;
  7. Oracle JDK is licensed under the binary code license agreement, and OpenJDK is licensed under the GPL v2 license.

5. The difference between Java and C++

I know that many people have never learned C++, but the interviewer just likes to compare Java and C++! no way! ! ! Even if you have never learned C++, write it down!

1. Both are object-oriented languages ​​and support encapsulation, inheritance, and polymorphism. Java does not provide pointers to directly access memory, and program memory is more secure

2. Java classes are single inheritance, C++ supports multiple inheritance; although Java classes cannot be multiple inheritance, interfaces can be multiple inheritance.

3. Java has an automatic memory management mechanism that does not require programmers to manually release useless memory

6. What is the main class of a Java program? What is the difference between the main class of an application and an applet?

There can be multiple classes in a program, but only one class can be the main class. In a Java application, this main class refers to the class that contains the main() method. In Java applets, this main class is a subclass inherited from the system class JApplet or Applet. The main class of the application is not necessarily required to be a public class, but the main class of the applet must be a public class. The main class is the entry point for Java program execution.

7. What are the differences between Java applications and applets

Simply put, the application is started from the main thread (that is, the main() method). The applet applet has no main method. It is mainly embedded in the browser page to run (call init() thread or run() to start). The embedding in the browser is similar to the flash game.

8... The difference between character constants and string constants

1. Formally: A character constant is a character surrounded by single quotation marks. A string constant is a number of characters surrounded by double quotation marks.

  1. Meaning: A character constant is equivalent to an integer value (ASCII value), which can participate in expression operations. A string constant represents an address value (the string is stored in memory)

  2. Occupy the memory size character constant only occupies 2 bytes string constant occupies several bytes (at least one character end mark) (Note: char occupies two bytes in Java)

9. Can Constructor be overridden

When talking about inheritance, we know that the private properties and construction methods of the parent class cannot be inherited, so the Constructor cannot be overridden, but it can be overloaded, so you can see that there are The case of multiple constructors.

10. The difference between overloading and rewriting

Overloading : Occurs in the same class, the method name must be the same, the parameter type is different, the number is different, the order is different, the method return value and access modifier can be different, and it occurs at compile time.
  
Rewrite : Occurs in the parent and child classes, the method name and parameter list must be the same, the return value range is less than or equal to the parent class, the thrown exception range is less than or equal to the parent class, and the access modifier range is greater than or equal to the parent class; if the parent class method access modification If the symbol is private, the subclass cannot override this method.

Follow me and reply to "learning" in a private message to receive a summary of Java interview questions from major manufacturers + Java knowledge points learning mind map + a 286-page pdf document summary of Java core knowledge points!
Welcome everyone to learn and exchange together. If you like the article, remember to follow me and give me a like, thank you for your support!

Guess you like

Origin blog.csdn.net/weixin_50333534/article/details/108600844
Recommended