1. Java basic skills
1.1. Introduction to Java (basic concepts and common sense)
1.1.1. What are the characteristics of the Java language?
1. Easy to learn;
2. Object-oriented (encapsulation, inheritance, polymorphism);
3. Platform independence (Java virtual machine realizes platform independence);
6. 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);
7. 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);
8. Compilation and interpretation coexist;
1.1.2. The most detailed and popular answers about JVM JDK and JRE
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.
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 is not oriented to any specific processor, but only oriented to the virtual machine. The Java language uses bytecode to solve the problem of low execution efficiency of traditional interpreted languages to a certain extent, 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 computers with multiple different operating systems without recompilation.
There are generally three steps from source code to running of a Java program:
What we need to pay special attention to is 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 needed to be called (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 saves the machine code corresponding to the bytecode, and 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.
to sum up:
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 Java language "compile once, run anywhere".
184.108.40.206. JDK and JRE
JDK is the Java Development Kit, which is a fully functional Java SDK. It has everything the JRE has, as well as the compiler (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 an 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.
1.1.3. Comparison of Oracle JDK and OpenJDK
Maybe many people like me have not touched or used OpenJDK before looking at this issue. So is there a major difference between Oracle and OpenJDK? Below I will use some of the information I have collected to answer this question that has been overlooked 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 and is maintained by Oracle engineers. Regarding the difference between JVM, JDK, JRE and OpenJDK, an Oracle blog post in 2012 has a more detailed answer:
to sum up:
1. The major version of Oracle JDK is released approximately every 6 months, while the OpenJDK version is released approximately every three months. But this is not fixed, I think it is useless to understand this.
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 are 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. In terms of responsiveness and JVM performance, Oracle JDK provides better performance than OpenJDK;
5. 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;
6. Oracle JDK is licensed under the Binary Code License Agreement, and OpenJDK is licensed under the GPL v2 license.
1.1.4. What is the difference between Java and C++?
I know that many people have never studied C++, but the interviewer just likes to compare Java and C++! no way! ! ! Even if you haven't learned C++, write it down!
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
Java classes are single inheritance, and C++ supports multiple inheritance; although Java classes cannot be multiple inheritance, interfaces can be multiple inheritance.
Java has an automatic memory management mechanism, which does not require programmers to manually release useless memory
In C language, a string or character array will have an extra character'\0' at the end to indicate the end. However, there is no concept of terminator in the Java language.
1.1.5. What is the main class of a Java program. What is the difference between the main class of an application and a small program?
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 the execution of the Java program.
1.1.6. 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() or run() to start). The embedding in the browser is similar to the ﬂash mini game.
1.1.7. What is the difference between import java and javax?
At the beginning, the required package for JavaAPI was the package beginning with java. At that time, javax was only used to extend the API package. However, over time, javax has gradually expanded to become an integral part of the Java API. However, moving the extension from the javax package to the java package is really too cumbersome and will eventually break a bunch of existing code. Therefore, it was finally decided that the javax package would become part of the standard API. So, there is actually no difference between java and javax. It's all a name.
1.1.8. Why is the Java language "compilation and interpretation coexisting"?
High-level programming languages are divided into two types: compiled and interpreted according to the way the program is executed. In simple terms, a compiled language refers to a compiler that translates source code into machine code that can be executed by the platform at one time for a specific operating system; an interpreted language refers to a machine where the interpreter interprets the source program line by line into a specific platform. Code and execute immediately. For example, if you want to read an English classic, you can find an English translator to help you read it. There are two options. You can first wait for the translator to translate the entire English classic (that is, the source code) into Chinese, and then To read, you can also have a translator translate a paragraph, you read a paragraph next to it, and slowly finish the book.
The Java language has both the characteristics of a compiled language and an interpreted language, because the Java program has to be compiled first and then interpreted in two steps. The program written by Java needs to go through the compilation step first to generate bytecode (*. class file), this kind of bytecode must be interpreted and executed by a Java interpreter. Therefore, we can think of the coexistence of Java language compilation and interpretation.