First talks Java language overview

xiaoxiao2021-03-06  63

★ First talks Java language overview

[Before class think] 1. Does a new language need to learn from the previous programming language? 2. Is there any other cross-platform language before the Java language? 3. What programming languages ​​are object-oriented? And which programming languages ​​are facing process? What is the essence difference in programming ideas? Is the C language to object-oriented or the process? 4. Is an excellent program code be extremely readable? Should the programmer should follow the same programming specification? 5. A programmer's compilation code If you need to make other programmers, how to provide the code instructions for this code?

1.1 History of development of Java language

1.1.1 Java language gains huge success in the Internet era

The Java language has a safe, cross-platform, object-oriented, simple, suitable for networking, etc., and Java languages ​​have become the most popular network programming language.

After experiencing a centralized computing mode represented by a large machine and a dispersion computing mode represented by a PC, the Internet has entered the network calculation era. One feature of the network computing mode is that the computer is heterogeneous, that is, the computer's type and operating system is different. For example, the hardware of the Sun workstation is a SPARC system. The software is the Solaris operating system in UNIX, and the hardware of the PC is Intel. System, operating system is Windows or Linux. Another feature of the network computing mode is that the code can be migrated over a variety of computers over the network, which urgently requires a cross-platform programming language that makes the program written in the network to be able to operate normally on various computers in the network. Java is born in this requirement.

1.1.2 Production of Java Language

The C language is a process-oriented language, is also a language that is very useful; and after the object-oriented ideology is introduced to the programming language, the C language is transformed into an object-oriented C language and has been widely used. However, the C language must be compatible with the C language, so the C language is a procedure and object-oriented language.

After the Java language is generated in the C language, it is a completely object-oriented programming language, fully absorbed the advantages of C language, using many grammar familiar with the C language familiar to programmakers, and remove the pointer in the C language, memory Application and release, etc. affect the part of the robustness.

A goal of the Java language is cross-platform, so it is interpreted as the current machine code as the current machine code in accordance with the different hardware platforms in the execution process, using the different hardware platforms in the execution of the different hardware platforms to implement cross-platform operations. The mechanism for dynamically downloading program code is completely characterized by the characteristics of network computing. The program can download the code from the server from the server as needed, and there is no language to support this.

1.2 Java working principle

1.2.1 Java Virtual Machine (1)

Java virtual machines are software simulated computers that can be safely and compatible on any processor (whether in the computer or in other electronic devices), the bytecode in the .class file. The "Machine Code" of the Java virtual machine is saved in the .class file, sometimes it can also be called a band code file. The cross-platform of the Java program is mainly referring to the characterization code files can be run on any computer or electronic device with a Java virtual machine. The Java Interpreter (Java Command) in the Java Virtual Machine is responsible for interpreting the bytecode file as a specific machine. The code is running.

1.2.1 Java Virtual Machine (2)

However, the establishment of Java virtual machines requires special implementation for different hardware and software platforms, and must consider the type of processor, and consider the type of operating system. As shown in the following figure, there are currently implemented on UNIX, Linux, Windows, and some real-time operating systems in embedded processing chips such as SPARC structure, X86, MIPS, and PPC. , 1.2.2 Using Iless Memory Automatic Recycling Mechanism

In the Java operating environment, there is always a system-level thread that tracks memory usage, regularly detects memory, automatic recycling, avoiding the disclosure of memory, also alleviating programmers's workload .

1.2.3 Code Security Checking Matter Nature The execution requires three steps, first of all the class loader (Class Loader) is responsible for loading class files (.class files) into the Java virtual machine, this process needs to be checked Whether the class file complies with the class file specification; the second-bytecode verifier checks if there is some illegal operations in the code of the class file, such as the operation of the machine file system in the applet program; if the bytecode code The verifier check passes, and the Java interpreter is responsible for interpreting this class file as a machine code. The Java virtual machine uses the "sandbox" operating mode, that is, limit the code and data of the Java program to a certain memory space, not allowed to access the memory outside the memory space, if it is an Applet program, not allowed access The file system of the client machine.

1.2.4 Characteristics of Java language

Comparison of Java and C / C languages:

a. The global variable Java program cannot define the global variable of the program, and the public and static variables in the class are equivalent to the global variables of this class. This makes the global variable package in the class, guaranteed security, and in the C / C language, due to the unpaged global variables often cause the system to crash due to improper use. b. Condition transfer command C / C language uses the goto statement unconditional jump, while the Java language does not have a goto language, replaced by exception processing statements try, catch, finally, improves the readability of the program, and also enhances the programs Rod. C. The pointer pointer is the most flexible in the C / C language, but it is also the most prone to error. Memory operation with pointers tend to cause unpredictable errors, and by means of display type conversion for the memory address, it can be classified private members to destroy security. In Java, the programmer cannot perform any pointer, and the array in Java is implemented by class, which is well solved the shortcomings of the C / C language of the array crosstore. d. Memory Management In the C language, programmers use library functions Malloc () and Free () to assign and release memory, and C language is operator New and Delete. Release the released memory block or release the memory that is not allocated, causing the crash of the system, and forgetting the memory block that is no longer used will gradually exhaust system resources. In Java, all data structures are objects, allocating memory through operator NEW, and obtains the right to use objects. The useless memory recycling mechanism guarantees the complete system resources and avoids the system crashes caused by memory management. e. The consistency of data types In a C / C language, on a different platform, the compiler assigns different bytes to simple data types such as int, float, and the like. For example: INT is 16 bits on the IBM PC, and the VAX-11 is 32 bits, resulting in the unavigified code data. In Java, the bit number assignment of the data type is always fixed, regardless of it on any computer platform. Therefore, the platform independent and portability of Java data is guaranteed. f. Type Conversion In the C / C language, you can make any type conversion through pointers, and unsafe factors greatly increase. In the Java language, the system should strict compatibility checking for the processing of objects to prevent unsafe conversion. g. The header file uses the prototype and global variable and library function of the header file in the C / C language. It is very difficult to maintain these headers in large systems. Java does not support header files, the types of members and access are packaged in a class, and the runtime system controls access to prevent illegal access. At the same time, Java communicates with other classes in Java to access other classes. h. Structure and joint C / C language use structures and combinations to represent a certain data structure, but due to their members, there is a problem. Java does not support structural and union, and the data structure and the operation of the data are encapsulated in the class. i. Prerequisites macro definition in the C / C language, and the code implemented with macro is often affecting the readability of the program, while Java does not support macro definitions.

1.2.5 Java platform - Continuously extended computing platform Java is not only a programming language, but also a development platform, Java technology provides many tools: compilers, interpreters, document generators, and file packaging tools, etc. At the same time, Java is still a program publishing platform, there are two main "released environments", first of all Java runtime environments, referred to as a complete class file package, followed by many of the main browsers to explain Java interpretation Merchants and runtime environments. At present, Sun is divided into JAVA platform into J2EE, J2SE, J2ME three platforms, and positions different market objectives and equipment. J2EE is Java2 Enterprise Edition, the main purpose is to provide an application server's operation and development platform for enterprise computing. J2EE itself is an open standard, any software vendor can launch its own products that meet J2EE standards, and users can have multiple options. 29 of IBM, Oracle, BEA, and HP have launched their own products, especially with BEA's WegLogic products and IBM's WebSphare. J2EE will gradually develop into a network computing platform that can be resistant to Microsoft .NET strategy. J2se is Java2 Standard Edition, main purpose is to provide a platform for desktops and workstations to provide a platform for development and running. In the process of learning Java, we mainly use J2SE to develop. J2ME is Java2 Micro Edition, mainly for consumer electronics, providing a Java run platform for consumer electronics, making Java programs to run on mobile phones, set-top boxes, PDAs and other products. The relationship between the three Java platforms described above is shown on the right. 1.3 Everything is an object

The first principle of object-oriented is to package data and the operation of the data in a class, and consider multiple objects and their relationships between the program when designing.

Pure object-oriented programming method is like this:

1) All things are objects.

2) The program is a combination of a large number of objects.

3) Each object has its own storage space.

4) Each object belongs to a class.

1.4 Building a Java program

The Java program is divided into two kinds in Java Application (Java App) and Java Applets. If the code of this file is used in the additional class provided by the system, an Import statement must be placed. Say that it is an additional means that a special class library "java.lang" will automatically import each Java file.

1.4.1 The first Java ApplicationImport java.util. *;

/ * Below we use two classes of Date and Properties, which belongs to Java.util this package; * / / * and both classes of System and Runtime are Java.lang this package. * /

Public class print {public static void main (string args []) {system.out.println (new date ()); // Print the date Properties P = System.getProperties () in the command line below; // Get the system Properties object P,

// getProperties () is a static method of the System class (STIC method), because it is "static", so you don't have to create

// The method can call this method p.List (System.out); // list (), it sends its entire content to a printstream object System.out.println ("--- Memory Usage: "); Runtime = runtime.getRuntime (); // creates a runtime by calling runtime, including memory and other information system.out.println (" Total Memory = " RT.TotalMemory () // Print total memory size "free memory =" rt.FreeMemory ()); // Print free memory size}}

1.4.2 First Java Applet

Applet doesn't have a main () method, which must be embedded in hypertext files and run in the browser. All Applet programs are subclats of the Applet class, and the Applet program is executed from method init (). The PAINT method is inherited in its ancestor class Component, and the method is called when Component is first displayed or sang. The parameter graphics is the object being displayed.

// This is our first Java Applet, which is saved in the file helloeducation.java Import java.awt.graphics; import java.applet.applet;

Public class helloeducation extends applet {

Public string S; public void init () {s = new string ("Welcome to Tongfang Education");} public void Paint (Graphics G) {g.drawstring (s, 25, 25); // Coordinate in the browser String s} (25, 25)

The Java Applet program is also a class, which compiles the same as Java Application. The execution of the Java Applet is completely different from the Java Application. The Java Applet program must be embedded in the HTML file to be executed, so the corresponding HTML file must be written.

The following is the content of the HelloeduCaiton.html file:

You can then perform the Java Applet program under the command line by the command "AppletViewer" provided by JDK. If it is in a Windows operating system, you can type "AppletViewer HelloEducation.html" under the "Command Prompt". Applets can also be run in another way, that is, open the HelloEducation.html program directly in the browser, in mainstream browsers such as IE, Netscape contains Java virtual machines, responsible for explaining the execution Java Applet program.

1.5 Java program specification

1.5.1 Java Source Structure A complete Java source program should include the following sections: Package statement; // This section only one sentence, must be placed in the first sentence of the source program; / * This section can have several IMPORT statements Or do not, you must put it before all class definitions * / public classdefinition; // public class definition section, only one public class definition // java language regulates the file name of the Java source program must be completely consistent with the public class name ClassDefinition; // Class definition section, you can have 0 or more class definitions interface, // interface definition part, can have 0 or more interface definitions, for example, a Java source program can be a structure, the source program named helloworldApp .java: package javawork.helloworld; / * Put all the .class files generated in the package javawork.HelloWorld * / Import java.aworld. *; // tell the compiler This program uses the system AWT package Import Javawork .newcent; / * Tell the compiler This program uses the user-defined package javawork.newcent product * / public class helloworldapp {...} / * The definition of the public class HelloWorldApp, the name is the same * / Class ThefirstClass {... // The definition of the first ordinary class ThefirstClass Class theSecondClass {...} // The second ordinary class THESECONDCLASS definition ... // Other ordinary classes Define Interface ThefirstInterface {...} / * The first interface ThefirstInterface is defined * / ... // Other interface definition package statement: Package in the actual implementation process is corresponding to the file system For example, the directory corresponding to javawork.helloworld is Path / Java Work / HelloWorld, and PATH is specified when compiling the source program. For example, when compiling the above helloworldapp.java files in the command line, you can type "javac -df: / javaproject hellloworldapp.java" in the command line, then build the generated helloWorldapp.class file will be placed in the directory F: / JavaProject / Javawork / HELLOWORLD / directory below, at this time f: / javaprojcet is equivalent to Path. But if you do not specify a path when compile, the generated .class file will be placed under the current directory where the command line is located. For example, in the command line directory f: / javaproject, the build command "Javac HelloWorldapp.java" is knocked, the generated helloworldapp.class file will be placed under the directory F: / JavaProject, and the package statement is equivalent to the work.

IMPORT statement: If you use a class other than Java.lang this package in the source program, whether the system's class or the class in its own defined package, must be identified with the import statement to notify the compiler to find the corresponding Class file.

Naming Rules for Source File: If there is a definition of public class in the source program, the source file name must be exactly the same as the name of the public class, and the case in the letter must be the same. This is a strict regulations in the Java language. If you do not follow, you will be wrong when compiling. Therefore, there can be only one public class definition in a Java source program. If the definition of the public class is not included in the source program, the file name can be named any. If there are multiple class definitions and interface definitions in a source program, a .class file will be generated for each class when compiling. (Generate .class files after each interface is also generated 1.5.2 Java Programming Specification Package: Package is fully lower-written, intermediate can be separated by points, for example: java.awt.event; Class Name: First letters Understand, usually synthesized by multiple words, requiring the first letter of each word, such as Class HelloWorldApp; interface name: Naming rules is the same as class name, such as Interface Collection; method name: often synthesized by multiple words The first word is usually verb, the first letters lowercase, the first letter of each word in the middle must be capitalized, for example: balanceAccount, isbuttonpressed; variable name: Full lower case, generally noun, for example: length; constant name: basic data The constant name of the type is all uppercase. If it is made up of multiple words, it can be spaced apart from the underline, for example: int year, int week_of_month; if it is the constant of the object type, it is case-sensitive, separated by uppercase letters . 1.5.3 Java Note

Single line of comments: From "//" to the end of the Bank, it is a comment.

Multi-line comments: All content between "/ *" and "* /" is comments,

Document Note: Document Note Way does not have a C / C in Java in terms of comments. Its core thinking is that after the programmer has completed the program, the Javadoc command provided by JDK can generate the programmed API document, and the content in this document is primarily extracted from the document comment. The API document appears in the form of an HTML file, which is exactly the same as the style and form of Java Help documents. Anything between "/ **" and "* /" is a document comment. For example, the following DOCTEST.JAVA file:

/ ** This is an example of a document annotation. Here, the following class * / public class doctest {/ ** variable comment, the following variable is mainly acting as an integer count * / public int i; / ** method comment, below The main function of the method is to count * / public void count () {}}

Run "javadoc -d./doc doctest.java", which generates files such as Index.html in the current DOC directory.

转载请注明原文地址:https://www.9cbs.com/read-118258.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.040, SQL: 9