1. Features of the Java language
1.1. Simplicity
-
The real memory manipulation in the Java language: the JVM (Java Virtual Machine)
-
All Java programs run in a Java virtual machine.
-
The Java virtual machine operates on memory during execution.
-
For C or C++ programmers can manipulate memory directly through Pointers.
-
C or C++ is more flexible and allows the programmer to manipulate memory directly, but requires a skilled programmer.
-
C or C++ is more manipulative.
-
The Java language blocks the concept of Pointers, and programmers cannot directly manipulate Pointers, or programmers
-
Memory cannot be manipulated directly. This approach has advantages and disadvantages: Advantages: It is not easy to cause memory leaks. (Easy.) Cons: Efficiency issues, including poor steering.
Airplane navigation: if it is C language that programmers are airplane pilots. If it's Java, the programmer is a passenger on the plane.Copy the code
-
The underlying Java language is C++, so the JVM is a virtual computer written in C++.
-
Where is the JVM? Once the JDK is installed, the JVM is ready.
-
What is memory? What are the most important components of a computer? CPU: Central processing unit, equivalent to the human brain, responsible for sending and executing instructions. Is the commander of the entire computer. The CPU is responsible for computing, responsible for computing. 10 + 20 = 30 The CPU is responsible for calculating 30. But in the calculation process, there are three data need to find a temporary space to store: these three data are: 10, 20, 30
Memory: Temporary storage space for data while a program is running. Data in memory disappears after a power failure or shutdown. Hard disk: Persistent device. Data on the hard disk will not be lost due to power outages. Motherboard: equivalent to the human torso, is a carrier: CPU, memory, hard disk and other major components are placed on the motherboard, the motherboard has a lot of lines, the above parts link up. .Copy the code
1.2. Java is completely object-oriented.
- Object orientation is easier to understand, and humans generally perceive the world as objects.
- Adopting an object-oriented approach can simplify complex problems.
1.3. Robustness
- This is mainly because there is a mechanism in Java: automatic garbage collection (GC).
- The Java language is robust, and C is not as robust as Java.
- Java is not prone to memory leaks.
- Using C++ or C incorrectly can easily lead to memory leaks.
- The JVM is responsible for scheduling the GC mechanism. Programmers don’t need to interfere.
- JVM (a virtual computer written in C++), GC (garbage collection mechanism)
1.4. Java fully/perfectly supports multithreaded concurrency.
1.5. Portability/cross-platform
- The Java language can be written once and run anywhere.
- For example, after Java programs are written, they can run on Windows, Linux and MaC OS without any changes.
- Write it once, run it anywhere. (The platform changes, but the program doesn’t.)
- What are the advantages and disadvantages of the JVM as a mechanism for cross-platform implementation? Advantages: Write once and run anywhere, cross-platform. Cons: Trouble. To run Java programs, you must have a JVM.
- In the same way that you have to install flash to watch videos on the web.
- Can the Java language write a virus? Yes, no problem. But it’s hard to poison users. It’s usually Java programmers who get poisoned. So few people write Java virus scripts.
JDK, JRE, AND JVM?
- JDK:Java development toolkit
- JRE: Indicates the Java runtime environment
- The JVM, the Java virtual machine
- The JDK includes the JRE and the JRE includes the JVM.
- JVMS cannot be installed independently.
- Both JRE and JDK can be installed independently.
- There is a separate JDK installation package.
- There is also a separate JRE installation package.
- There is no separate JVM installation package.
- When you install the JDK: the JRE is automatically installed, and the JVM inside the JRE is automatically installed.
- When installing the JRE: The JVM is automatically installed.
- Q: Suppose you have developed a new piece of software at a software company and are going to a client’s office to deploy the project to get it up and running. Do you need to install the JDK? You just need to install the JRE. The JRE is small in size and easy to install.
- Q: Why does JDK installation come with a JRE? Because after a Java programmer develops a program, to test the program and make it run, he needs a JRE. So JDK installation comes with an internal JRE.
3. Summarize the key terms we have been exposed to so far:
Java architecture technologies are divided into three parts: JavaSE: standard JavaEE: Enterprise JavaME: micro version After JDK installation: JDK: Java development toolkit JRE: Java runtime environment JVM: Java VIRTUAL machineCopy the code
4. Understanding of Java loading and execution (theory is important)
- Java program from writing to the final run through the process ????
- The two most important phases of a Java program are the compile phase and the run phase
- Note: Java code written directly by a Java programmer (plain text) cannot execute as recognized by the JVM. Java code written by Java programmers as plain text must go through a compilation that turns “plain text code” into “bytecode,” which the JVM can recognize.
- The process by which Java code becomes bytecode from plain text is called compilation.
- Java code this plain text is called Java source code. (The code you write is source code.)
- The source code cannot be executed directly; it must be compiled first, and the resulting “bytecode” is the bytecode that the JVM recognizes.
- Can compile phase and run phase be done on different operating systems?
You compile it on Windows and you generate bytecode and you can run bytecode on Linux because Java is cross-platform. You can write it all at once.Copy the code
- Can the Java source code be deleted once compiled? Can only bytecode be executed?
Completely executable, because the source code does not participate in the program's execution process. What is involved in the execution of a program is bytecode. But it's best not to delete the source code. Because it is possible that the execution result is not what you need, when the execution result is not what you need, you can reopen the source code to make changes, then recompile to generate new bytecode, and then execute again. This will have a new execution effect.Copy the code
- The file extension for the source code must be xxx.java
- Note also that the bytecode file extension generated by compilation is xxx.class
- The.java file is the source file in which the source code is written.
- The.class files are bytecode files, which are compiled from source code.
- Another thing to note:
A single Java source file can be compiled to generate multiple class files. The final thing to run is the class file.Copy the code
- Question: Are bytecode files binary?
Bytecode files are not binaries. If it's binary, you don't need a JVM. Because the operating system can execute binary directly.Copy the code
- How does a Java program go from development to final execution?
Compile time: (on Windows) Step 1: Create a new XXX.java file somewhere on your hard disk (optional) Step 2: Open the xxx.java file using Notepad or another text editor such as EditPlus step 3: Write the "Java syntactic" source code in the xxx.java file. The fourth step: save (must save the XXx. Java file) the fifth step: use the compiler (javac [JDK installation comes with]) to compile the XXx. Java file. Step 6: If the source code written in the xxx. Java file is syntactic, the compilation will pass. If the source code written in the xxx. Java file is syntactic, the compiler will report an errorclassThe file is not generated, only if it is compiledclassBytecode files. And ajavaSource files can be generated in multiple waysclassFile. (compilation is essentially checking syntax) run time :(JREIt's at workwindowsOn, but also on otherOS). Step 7: If it's inLinuxTo run, you need to setwindowsGenerated on theclassYou don't need to copy the source code to copy the files. What's really running is bytecode. (But don't delete the source code, it works.) step 8: UseJDKA built-in command/tool:javaExecute bytecode step 9: Take care of the restJVMThere is no need for programmers to interfere.JVMIt loads the bytecode file in, and thenJVMInterpreting bytecodes (the interpreter is responsible for interpreting bytecodes as 1010101010.. Step 10:JVMWill hand over the generated binary codeOSOperating system, operating system will execute binary code to interact with the hardware.Copy the code
- Note: There are two very important commands that need to be used in this process. 1. Compile the javac command. 2. Run the Java command
- Episode:
Java source files are compiled to generate a.class, B.class, c. class, etc., so we call A A class, B A class, C A class. Where A, B, and C are the class names. No reason, memorization, that's what SUN's Java syntax dictates. A/B/C is the name of the class. Class A, B and C.Copy the code
- The code written in the source file is called source code.
- That’s a complicated process, so what’s a programmer supposed to do?
Create a Java file Open a Java file Write Java source code save javac command compile Java command run Write, compile, and runCopy the code
5. Write the first Java program in Java: HelloWorld
- You don’t have to understand this program, you just have to copy it, because I’m not going to explain why it’s written that way.
- Mainly in order to build Java development environment, test whether the Java environment can be used.
Step 1: Install the text editor (EditPlus) step 2: Install JDK (download JDK first) install JDK13, go to the next step. JDK13 is installed with a built-in JRE, and a JRE independent of the JDK is not generated. For java13, you need to execute special commands if you want to generate a JRE independent of the JDK. I won't talk about it here, but I'll talk about it later when I use it. Note that not only does JDK8 install with a built-in JRE, but it also generates a separate JRE outside the JDK directory. (In earlier versions, JRE was actually available2A). One is built into the JDK and one is independent of the JDK. Exe is responsible for compiling java.exe is responsible for running Java.exe. Step 3: Write the first program HelloWorld. This code is emphasized here: file name copy file content code copy case copy any link copy punctuation parentheses: [] () {} should be written in pairs. Step 4: Compile Step 5: RunCopy the code