- Java program running process
- 2. Comments in java
- 3. Explain the HelloWorld program
- 4. The difference between public class and class:
- Two, java language foundation
- Identifier:
- Keywords:
- Literal value:
- variable
- Three, data types in java
- Processes and Threads
- Processes
- Threads
- ava program compilation and running process
- compilation process
- working process
- 1. Class loading
- 2. JVM finds the main() method entry
- 3. Instantiate the object
- 4. How to run
- Replenish
Java program running process
——How to use javac.exe
javac A.java generates compiled file A.class
——How to use java.exe: java class name E.g: There is an A.class on the hard disk, then java A can be used There is a B.class on the hard disk, then you can use java B Cannot be written as java A.class ——The process of operation phase is: * Open a DOS command window * Input: java A * The java.exe command will start the Java Virtual Machine (JVM), and the JVM will start the class loader ClassLoader * ClassLoader will search for the A.class file on the hard disk, and load the bytecode file into the JVM when the modified file is found. * JVM interprets the A.class bytecode file into binary data like 1010101010. * Then the operating system executes binary files to interact with the underlying hardware platform Introduction to the bin directory: javac.exe is responsible for the compilation (when there is Chinese in the file, you need to add: javac -encoding utf-8 HelloWorld.java) java.exe is responsible for running
2. Comments in java
* What is a comment? What is the purpose of a comment? —— Appears in the java source program, explaining the java source code —— Comments will not be compiled into .class bytecode files —— A good development habit should be to write more comments, so that the readability of the program will be improved * How to write comments in java? ——Single line comment //Single line comment, only comment the current line ——Multi-line comment /* Multi-line comments Multi-line comments Multi-line comments Multi-line comments */ ——javadoc comment /** * Multi-line comment * Multi-line comment * Multi-line comment * Multi-line comment **/ Note: This kind of comment is a more professional comment, which will be parsed by javadoc.exe and generate a help document
3. Explain the HelloWorld program
public class HelloWorld public static void main(String[] args) System.out.println("Hello World!") > >
Need to remember:
public
class
static
void
System.out.println(); Output messages to the console
Class body
Method body
Java statements cannot be written directly in the class body [except for declaring variables]
A java statement must end with a semicolon (;)
You can write multiple java statements in the method body
The main method is the program entry
4. The difference between public class and class:
Multiple classes can be defined in a java source file
class A<>
class B<>
The public class in a java source file is not necessary
A class will define and generate a xxx.class bytecode file
Only one public class name can be defined, and it must be the same as the file name
In each class, the main method can be written, and the entry of the program can be set, and different classes can be executed after compilation.
When executing a class in the window, the class must have a main method, otherwise an error will be reported
public static void main(String[] args) >
Two, java language foundation
OOA: Object-oriented analysis
OOD: Object-oriented design
OOP: Object-oriented programming
Identifier:
Keywords:
Literal value:
variable
Three, data types in java
- What is the role of data types? There are a lot of data in the program, and each data has related types, and different types of data occupy different amounts of space.
The function of the data type is to guide the JVM how much memory space is allocated for the data when running the program. - There are two types of data in java:Basic data type
Reference data type - About basic data types The basic data types include four categories and eight categories:
The first type: integer type
byte,short,int,long
The second category: floating point
float,double
The third category: Boolean
boolean
The fourth category: character type
char - The string «abc» is not a basic data type, but a reference data type String use double quotes «»
characters use single quotes’’ - What is the space occupied by each of the eight basic data types?
Basic data type Memory used (bytes) byte 1 short 2 int 4 long 8 float 4 double 8 boolean 1 char 2 - Byte:
1 Byte = 8 bit [1 byte = 8 bits] 1 bit represents a binary bit: 1/0
1KB = 1024 Byte
1MB = 1024 KB
1GB = 1024 MB
1TB = 1024 GB 1TB = 1024 *1024 * 1024 * 1024 * 8 bit - The byte type in the integer type occupies 1 byte, so the byte type data occupies 8 bits, so what is the value range of the byte type?
Regarding the number types in java, numbers are divided into positive and negative, so there is a binary bit in the binary of the number called the sign bit.
And this sign bit is on the leftmost side of all binary bits. 0 means positive number, 1 means negative number.
Maximum value of byte type: 0111 1111
[00000000 00000000 00000000 10000000 (binary) -1]
The maximum value of byte type: 2 to the 7th power -1 127
The minimum value of byte type: -128
The value range of byte type: -127~128, which can represent 256 different numbers [256 different numbers].
Processes and Threads
In concurrent programming, there are two basic units of execution: processes and threads. In the Java programming language, concurrent programming is mostly concerned with threads. However, processes are also important.
A computer system normally has many active processes and threads. This is true even in systems that only have a single execution core, and thus only have one thread actually executing at any given moment. Processing time for a single core is shared among processes and threads through an OS feature called time slicing.
It’s becoming more and more common for computer systems to have multiple processors or processors with multiple execution cores. This greatly enhances a system’s capacity for concurrent execution of processes and threads but concurrency is possible even on simple systems, without multiple processors or execution cores.
Processes
A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.
Processes are often seen as synonymous with programs or applications. However, what the user sees as a single application may in fact be a set of cooperating processes. To facilitate communication between processes, most operating systems support Inter Process Communication (IPC) resources, such as pipes and sockets. IPC is used not just for communication between processes on the same system, but processes on different systems.
Most implementations of the Java virtual machine run as a single process. A Java application can create additional processes using a ProcessBuilder object. Multiprocess applications are beyond the scope of this lesson.
Threads
Threads are sometimes called lightweight processes. Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.
Threads exist within a process every process has at least one. Threads share the process’s resources, including memory and open files. This makes for efficient, but potentially problematic, communication.
Multithreaded execution is an essential feature of the Java platform. Every application has at least one thread or several, if you count «system» threads that do things like memory management and signal handling. But from the application programmer’s point of view, you start with just one thread, called the main thread. This thread has the ability to create additional threads, as we’ll demonstrate in the next section.
ava program compilation and running process
Java programs go through two major processes from the creation of the .java file to the execution of the program:
- .java files are compiled into .class files by the compiler
- Bytecode is interpreted and run by the JVM
compilation process
.java source files are compiled into .class files by the Java compiler:
- When Java compiles a class, if the class that this class depends on has not been compiled, the compiler will automatically compile the dependent class first, and then refer to it. If the Java compiler cannot find the .class file or .java source file of the class that the class depends on in the specified directory, it will report an exception error of «Cant found sysbol».
- The compiled .class file is mainly divided into two parts: the constant pool and the method table collection. The constant pool records the occurrences of the code (constants, class names, member variables, etc.) and symbol references (class references, method references, member variable references, etc.); the method table collection records the bytecode of each method.
working process
The JVM does not load all used classes into memory at runtime, but only loads them into the method area when they are used, and only loads them once. The process of running a Java class is roughly divided into two steps:
An example to illustrate the running process of a Java program:
public class Person < private String name;
public Person(String name) < this.name=name;
>
public void sayHello() System.out.println("Hello! My Name is: " + name);
>>
public class JVMTest < public static void main(String[] args) Person p=new Person("Li Ming");
p.sayHello();
>>
1. Class loading
First compile the JVMTest.java file to get the JVMTest.class file. The system starts a JVM process, finds the JVMTest.class file from the classpath path, and loads the JVMTest class information into the method area. This process is called JVMTest class loading.
(Only when the class information is in the method area, you can create an object and use the member variables in the class)
2. JVM finds the main() method entry
The main() method entry holds a pointer to the constant pool of the current class (JVMTest). The first item in the constant pool is a symbolic reference to the Person object.
In the main method `Person p=new Person(«Li Ming»), the JVM needs to create a Person object, but at this time there is no Person class information in the method area, so the JVM needs to load the Person class and load the Person class information into the method area.
The JVM replaces the symbolic reference to the first entry in the constant pool with a pointer directly to the Person class in the method area.
3. Instantiate the object
After loading the Person class information, the JVM allocates memory for a Person instance in the heap, then calls the constructor to initialize the Person instance, and the instance holds the type information (including the method table) pointing to the Person class in the method area. citations .
(p is a reference to the Person instance, which will be placed on the stack)
4. How to run
Execute p.sayHello(), the JVM finds the Person object according to the reference of p in the stack, and then locates the method table of the Person class information in the method area according to the reference held by the Person object , obtains the bytecode address of the sayHello method, and then starts run method.