Java BASICS (1): java Basics
JAVA Working Method
Source program (myProgram. java)-> compile (javac myProgram. java)-> JAVA bytecode (myProgram. class)-> Run (java myProgram)
Command:
- During Compilation: javac (compiler) + file name
- Runtime: java + file name
JAVA program structure
Source file> class> method> statement (source file> class> method> statement)
Import java. lang. String; import java. lang. System; public class MyFirstApp {public void main (String [] args) {System. out. print ("Hello World! ");}}
Note:
- The file name MyFirstApp must have the same class name and be case sensitive.
- It is case-insensitive when compiling with the javac command.
- It is case sensitive when running with java commands.
- Import java. lang can be omitted
- When the program starts, it will find the main () method, and main () is the starting point of the program.
- One program has one mian () method.
- A java program must have at least one class, but multiple
- JAVA is strongly typed, and the conditional test result must be a boolean value.
Differences between JDK, JRE, and JVM:
- JVM (Java Virtual Machine): Java Virtual Machine
- JDK (Java Developer's Kit): Java Development Kit
- JRE (Java runtime environment): Java runtime environment
Classification of programming languages (by program execution ):
1. Compile-type language: refers to the use of specialized compilers to translate source code into machine commands that computers can recognize at one time for a specific operating system. Such as C and C ++
2. interpreted language: refers to the use of a dedicated interpreter to interpret the source code one by one as a specific machine instruction, and to explain an execution sentence, similar to simultaneous translation. Such as ASP and PHP.
First knowledge of JVM and working principles:
JVM consists of four parts:
1. ClassLoader): Load the required class to JVM at JVM startup or during class running.
2. execution engine:Executes the bytecode commands contained in the class file.
3. Memory zone (also called runtime data zone ):It is used to operate the allocated memory zone when the JVM is running. The runtime memory zone can be divided into five areas,
- Method Area: used to store class structure information, including constant pools, static variables, constructors, and so on. Although the JVM specification describes the method area as a logical part of the heap, it has a non-heap (non-heap) alias, so do not confuse it. The method area also contains a runtime pool.
- Java Heap: the place where java instances or objects are stored. This is the main area of GC (explained later ). We can easily know from the stored content that the method area and heap are shared by all java threads.
- Java Stack: A java Stack is always associated with a thread. When a thread is created, JVM creates a corresponding java Stack for the thread. The java stack contains multiple stack frames. Each method is run to create a stack frame for storing local variable tables, Operation stacks, and method return values. The process from calling to execution of each method corresponds to the process from a stack frame to an outbound stack in the java stack. Therefore, java stacks are proprietary.
- PC Register: used to save the memory address of the current thread. Since the JVM program is executed in multiple threads (thread switching in turn), an independent counter is required to ensure that the thread can return to the original state after switching back, recording the previous interruptions. It can be seen that the program counter is also private to the thread.
- Native Method Stack is similar to java Stack, but serves native methods used by JVM.
4. Local method interface:It mainly calls local methods implemented by C or C ++ and returns results.
JVM is at the bottom layer of the entire JDK and is responsible for interacting with the operating system. It is used to shield the operating system environment and provide a Wang Zheng Java Runtime Environment. Therefore, it is also called a virtual computer. The operating system is installed with jvmthrough java.exe in JDK. The main steps are as follows:
1. Create a jvm loading environment and Configuration
2. Load jvm. dll
3. initialize jvm. dll
4. Call the JNIEnv instance to load and process the class
5. Run the java program
What is GC and how GC works: Garbage Collection and Garbage Collection
1.Generally, the garbage collector must detect and recycle garbage. How can I detect Spam? There are generally the following methods:
Reference counting method: adds a reference counter to an object. Every time a counter is referenced, 1 is added to the counter; if the reference is invalid, 1 is subtracted.
Accessibility Analysis Algorithm: search based on the root SET object. If an object is inaccessible, It is a junk object. The root set here generally includes objects referenced in the java stack, objects referenced in the constant pool in the Method Area, and objects referenced in local methods.
2.Why should we use the generational garbage collection policy?
In the process of running java programs, a large number of objects are generated. Because each object can assume different responsibilities and has different functions, it also has different lifecycles. Some objects have long lifecycles, for example, Session objects, threads, and Socket connections in Http requests; some objects have short lifecycles, such as String objects. Some objects can be recycled once because of their immutable class features. Imagine that, without differentiation of the object survival time, every garbage collection process recycles the entire heap space, which takes a long time, in addition, scanning for objects with a long survival time is futile. Therefore, we need to introduce the idea of divide governance. The so-called divide idea is to divide objects based on local conditions and put objects of different lifecycles on different generations to use different garbage collection methods.
3.How to divide it?
Objects are divided into Young Generation, Old Generation, and Permanent Generation according to their lifecycles ). The persistent generation mainly stores class information, so it has little to do with the collection of java objects, and is closely related to the collection of young and old generations.
4. Generational recovery is as follows: