Chapter 4 Introduction to Java technology-v512 workshop Editor: Xuan Yu
| What is Java |
Java Virtual Machine |
|
My blog |
| JDK installation Configuration |
Java Runtime Environment |
V512 Studio |
| Java program structure |
Java automatic garbage collection |
Chinese Emy of Sciences |
What is Java
Summary: Java is not only a programming language, but also a powerful and complete development and running environment.
The official website is http://java.sun.com. Sun (Stanford University Network) was established in February 1982 and currently employs more than 20 thousand employees worldwide
Development History: appeared in 1991. At that time, Oak was not called Java but also a programming language. It was mainly used for the development of embedded household appliances.
1995--05--23. Sun has made great improvements based on oak, and launched the first Java version java1.0.
The first Java Development Kit jdk1.0 (Java Development Kit)
May December 1998. Jdk1.2 was released and is called Java2. JDK 1.1 has made significant improvements based on JDK and, so sun changed Java to Java2 after JDK.
It indicates that Java after jdk1.2 has a completely different performance from the previous java. Therefore, it seems to be the second-generation Java product after jdk1.2.
Three platforms: to better adapt Java to the development of different programs, Sun has released three sub-platforms for different applications.
Java platform Micro Edition (Java me): Suitable for developing programs running on embedded household appliances and mobile phones
Java platform Standard Edition (Java SE): Suitable for developing applications running on the desktop
JAVA Platform Enterprise Edition (Java ee): Applicable to enterprise applications
Objective: To create an object-oriented programming language. With the development of technology, if you want to design a new programming language. If it is not object-oriented, it is basically difficult to obtain market recognition.
Provides an explanation environment for running programs to make program code independent from the platform. It is also a major feature of Java: independent from the platform and not related to any operating system. One write and run around
By absorbing the advantages of C and C ++, programmers can easily master it. Remove the components that affect program robustness in C and C ++, such as removing pointers, memory application and release, and unconditional transfer statements.
Multi-threaded mechanism is provided. Provides a mechanism for dynamic download of program code. Provides code verification mechanism to ensure security
Core Mechanism: the Java language provides three core mechanisms: Java virtual machine, garbage collection mechanism, and code security detection.
Java Virtual Machine (javavirtual
Machine): Java virtual machine can be used to implement platform independence of Java language.
Garbagecollection)
Codesecurity)
Operating principle: advanced programming languages can be compiled and interpreted, while Java is a combination of the two types. Three common terms need to be mentioned here: Java source code, bytecode file, and machine code command
That is to say, in Java, you first need to write the Java source file, and then use the tool javac in JDK to compile the source program into a bytecode file, that is, a file with the extension of class.
Then, use the Java commands in JDK to run the bytecode file, and then process the corresponding functions and display the output.
Program type: Java application: the main () method is used as the program entry and loaded and executed by the Java parser.
Java Applet: The main () method is not used as the program entry, and is loaded and executed by the browser or appletviewer.
During running, a small application needs to be embedded in HTML, and the browser loads HTML to call the program for running.
Compile and run: Compile the source program: Command: javac helloworld. Java (multiple java files can be compiled at the same time, such as javac A. Java test. Java)
(The source file is helloworld. Java, which is compiled by the javac compiler to generate bytecode files (. class files)
Explain and execute the bytecode file: Command: Java helloworld (interpreted and executed by the Java Runtime Environment (JRE)
Note: During execution, you cannot enter a password similar to this: Java helloworld. Class or Java helloworld. java.
Java Virtual Machine
A computer simulated by software or hardware on a computer. The Java Virtual Machine (JVM) reads and processes compiled platform-independent bytecode class files.
The Java compiler generates class files for Java virtual machines. Therefore, using JVM allows us to write programs independent of different platforms.
The Java interpreter runs the Java virtual machine code on a specific platform.
Byte code files compiled using the javac command with the extension of. class can be run on different operating platforms
The specific Runtime is executed on JVM suitable for different platforms. Therefore, the cross-platform Java language is implemented using JVM.
Java Runtime Environment (JRE: thejava
Runtime Environment)
Running environment composition: JRE = JVM + runtime interpreter + others
Differentiate between JRE and JDK: JRE is the Java Runtime Environment. Using JRE can run our class files.
JDK is a Java development environment. It not only allows Java to run programs, but also provides some functions required for Java development.
Generally, JDK includes the JRE function.
Three main functions: Code loading: completed by Class Loader
Verification Code: completed by bytecode Verifier
Code execution: completed by runtime Interpreter
Java automatic garbage collection (GC: garbagecollection)
Garbage Collection refers to the recovery of memory space that is no longer in use. In C/C ++ and other languages, programmers are responsible for recycling useless memory.
The Java language removes the programmer's responsibility to reclaim useless memory space. It provides a system-level thread to track the distribution of storage space.
When the JVM is idle, check and release the storage space that can be released. Java automatic garbage collection can reduce the burden of program development.
Garbage collection is automatically executed during Java program running, so programmers cannot precisely control and intervene.
Java program structure
Source File: If the Java source file contains a public-modified class, the name of the source file must be the same as that of the public class (case-insensitive) and Suffix:. Java
Number of classes: a single source file can only have one public class, and the number of other classes is not limited.
Case sensitivity: the spelling of words in Java is strictly case sensitive.
Part 1: the package statement is 0 or 1 and must be placed at the beginning of the file.
Part 2: The Import Statement is 0 or multiple, located after the package and before other components
Part 3: Public class definition // 0 or 1
Class Definition // 0 or more
Interface Definition // 0 or more
JDK installation configuration (including the Java Runtime Environment, compiler, corresponding APIs and other tools and resources)
Download JDK: Click javase, downloads, and download in jdk6update3 under technologies (or popuar downloads) on the Right of java.sun.com.
In the new page, you need to accept their protocol and click accept. Then, select jdk-6u3-windows-i586-p.exe for windows65.64mb.
Install JDK: after entering the custom installation interface during installation, select the content we need to install
① Development tools: Required
② Demo program and example: Required
③ Source code: refers to the source code of the jdk api.
④ Public JRE: Required
⑤ Java db: This is a newly added database written in Java by Sun. We don't need to install it.
The icons of these five optional functions all have downward arrow. You only need to select java db as "this function will be unavailable ".
After clicking "Next", a custom installation interface will appear, which will be installed by default, and then click "Next"
JDK Directory: Bin: includes the command tools required to run, compile, and execute specific Java applications.
DB: place the self-contained database program in Java, that is, the java db canceled during installation
Demo: some examples of Sun placement
Include: If a Java program calls some header files of the C/C ++ program
JRE: Jer in JDK
Lib: place some class libraries used by JDK
Sample: place other compiled example programs
Src.zip: All the API source code in JDK. For example, to download the stringclass in JDK, open src.zip and select Java → Lang → string. java.
Environment variables: After JDK is installed, you also need to configure path and classpath in "environment variables" under "advanced" in "My Computer attributes ".
Otherwise, the command prompt will prompt that 'javac' is not an internal or external command, or a program or batch processing file.
Java_home: D: \ Program Files \ Java \ jdk1.6.0 _ 13
Path: external command search path (value: JDK path \ bin or % java_home % \ bin. Add a semicolon after the bin)
Classpath: Class resource location search path (to search under the current path before running, it is customary to add the current path, that is, ".;")
Verify installation: after running Java-version in the command prompt, if Java version "1.6.0 _ 13" is output, the JDK is successfully installed.
Command Prompt: Start → program → attachment → command prompt. That is, the DOS terminal window, because we are currently developing programs running in the DOS terminal window
CD \: switch to the C root directory
Cd c: \ ex \ 01: switch to the 01 folder under the ex folder in drive C.
Dir: list the directories in the folder.
Dir filename. *: list all files named filename in the directory of this folder (in this case, the suffix is ignored)
Javac *. Java: compile all Java source files
Del *. Bak: delete all. BAK files under the folder.
MD P1: create a directory P1 or a folder p1
CLS: clear screen
Exit: Quit (quit is not the exit password)