官术网_书友最值得收藏!

1.4 Main Features of Java Programming Language

1.4.1 Portability

Most modern programming languages are designed to be (relatively) easy for people to write and to understand. These languages that were designed for people to read are called high-level languages. The language that the computer can directly understand is called machine language. Machine language or any language similar to machine language is called a low-level language. A program written in a high-level language, like Java, must be translated into a program in machine language before the program can be run. The program that does the translating (or at least most of the translating) is called a compiler and the translation process is called compiling. The Java compiler does not translate your program into the machine language for your particular computer. Instead, it translates your Java program into a language called byte-code(字節(jié)碼). Byte-code is not the machine language for any particular computer. Byte-code is the machine language for a fictitious computer called the Java Virtual Machine(Java虛擬機(jī)).

Java作為一種高級編程語言,具有很強(qiáng)的移植性,通過Java虛擬機(jī)可將字節(jié)碼運(yùn)行在不同的機(jī)器平臺上。

The reason is: only the JVM needs to be implemented for each platform. Once the run-time package exists for a given system, any Java program can run on it. Remember, although the details of the JVM will differ from platform to platform, all interpret the same Java bytecode. If a Java program were compiled to native code, then different versions of the same program would have to exist for each type of CPU connected to the Internet. This is, of course, not a feasible solution. Thus, the interpretation of bytecode is the easiest way to create truly portable programs. The fact that a Java program is interpreted also helps to make it secure. Because the execution of every Java program is under the control of the JVM, the JVM can contain the program and prevent it from generating side effects outside of the system. As you will see, safety is also enhanced by certain restrictions that exist in the Java language.

JVM是Java跨平臺的基礎(chǔ),Java源程序被編譯成字節(jié)碼,然后交給JVM解釋執(zhí)行。這樣Java就不需要知道自己是在何種硬件平臺下運(yùn)行的。

When a program is interpreted, it generally runs substantially slower than it would run if compiled to executable code. However, with Java, the differential between the two is not so great. The use of bytecode enables the Java run-time system to execute programs much faster than you might expect. Although Java was designed for interpretation, there is technically nothing about Java that prevents on-the-fly compilation of bytecode into native code. Along these lines, Sun supplies its Just In Time (JIT) compiler for bytecode, which is included in the Java 2 release. When the JIT compiler is part of the JVM, it compiles bytecode into executable code in real time, on a piece-by-piece, demand basis. It is important to understand that it is not possible to compile an entire Java program into executable code all at once, because Java performs various run-time checks that can be done only at run time. Instead, the JIT compiles code, as it is needed, during execution. However, the just-in-time approach still yields a significant performance boost. Even when dynamic compilation is applied to bytecode, the portability and safety features still apply, because the run-time system (which performs the compilation) still is in charge of the execution environment. Whether your Java program is actually interpreted in the traditional way or compiled on the fly, its functionality is the same.

JIT是JVM的一部分,它可以按要求即時(shí)地將字節(jié)碼逐一地編譯為可執(zhí)行的代碼。

1.4.2 Simple

We wanted to build a system that could be programmed easily without a lot of esoteric training and which leveraged today’s standard practice. Most programmers working these days use C, and most programmers doing object-oriented programming use C++. So even though we found that C++ was unsuitable, we designed Java as closely to C++ as possible in order to make the system more comprehensible.

Java omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. These omitted features primarily consist of operator overloading(although the Java language does have method overloading), multiple inheritance, and extensive automatic coercions.

We added automatic garbage collection, thereby simplifying the task of Java programming but making the system somewhat more complicated. A common source of complexity in many C and C++ applications is storage management: the allocation and freeing of memory. By virtue of having automatic garbage collection (periodic freeing of memory not being referenced) the Java language not only makes the programming task easier, it also dramatically cuts down on bugs.

Java是一種非常簡單的編程語言,優(yōu)化了C++語言中一些令人難以理解的特性,增加了諸如垃圾自動回收機(jī)制的內(nèi)容,方便了初學(xué)者的學(xué)習(xí)和使用。

1.4.3 Robust

The multi-plat formed environment of the Web places extraordinary demands on a program, because the program must execute reliably in a variety of systems. Thus, the ability to create robust programs was given a high priority in the design of Java. To gain reliability, Java restricts you in a few key areas, to force you to find your mistakes early in program development. At the same time, Java frees you from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks your code at compile time. However, it also checks your code at run time. In fact, many hard-to-track-down bugs that often turn up in hard-to-reproduce run-time situations are simply impossible to create in Java. Knowing that what you have written will behave in a predictable way under diverse conditions is a key feature of Java. To better understand how Java is robust, consider two of the main reasons for program failure: memory management mistakes and mishandled exceptional conditions (that is, run-time errors).

在多平臺環(huán)境中,健壯性是一個(gè)程序的重要指標(biāo),Java有著嚴(yán)格的語法規(guī)范,它可以在編譯時(shí)和運(yùn)行時(shí)檢查所編寫的代碼。Java主要考慮了會導(dǎo)致程序運(yùn)行失敗的兩個(gè)方面:內(nèi)存管理和不可處理的異常。

One of the advantages of a strongly typed language (like C++) is that it allows extensive compile-time checking so bugs can be found early. Unfortunately, C++ inherits a number of loopholes in compile-time(編譯期間) checking from C, which is relatively lax (particularly method/procedure declarations). In Java, we require declarations and do not support C-style implicit declarations. The linker understands the type system and repeats many of the type checks done by the compiler to guard against version mismatch problems. The single biggest difference between Java and C/C++ is that Java has a pointer model that eliminates the possibility of overwriting memory and corrupting data. Instead of pointer arithmetic, Java has true arrays. This allows subscript checking to be performed. In addition, it is not possible to turn an arbitrary integer into a pointer by casting.

Java程序在一些關(guān)鍵的地方做了強(qiáng)制性限制,這樣使開發(fā)者很容易發(fā)現(xiàn)自己所犯的錯(cuò)誤。同樣,Java也解除了很多開發(fā)者認(rèn)為可能會引起程序錯(cuò)誤的內(nèi)容。

1.4.4 Multithread

Java was designed to meet the real-world requirement of creating interactive, networked programs. To accomplish this, Java supports multithread programming, which allows you to write programs that do many things simultaneously(同時(shí)地). The Java run-time system comes with an elegant yet sophisticated solution for multiprocessor Synchronization(多處理器同步) that enables you to construct smoothly running interactive systems. Java’s easy-to-use approach to multithread allows you to think about the specific behavior of your program, not the multitasking subsystem.

Java提供易使用的多線程允許開發(fā)者考慮程序中所需要的特定行為。

1.4.5 Architecture-Neutral

A central issue for the Java designers was that of code longevity and portability. One of the main problems facing programmers is that no guarantee exists that if you write a program today, it will run tomorrow—even on the same machine. Operating system upgrades, processor upgrades, and changes in core system resources can all combine to make a program malfunction. The Java designers made several hard decisions in the Java language and the Java Virtual Machine in an attempt to alter this situation. Their goal was “write once, run anywhere, any time, forever.” To a great extent, this goal was accomplished.

體系結(jié)構(gòu)中性可以保證Java程序能在不同平臺上運(yùn)行,實(shí)現(xiàn)“一次編寫,處處運(yùn)行”的目標(biāo)。

1.4.6 Interpreted and High Performance

As described earlier, Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java byte code. This code can be interpreted on any system that provides a Java Virtual Machine. Most previous attempts at cross-platform solutions have done so at the expense of performance. Other interpreted systems, such as BASIC, TCL and PERL, suffer from almost insurmountable performance deficits. Java, however, was designed to perform well on very low-power CPUs. As explained earlier,the Java byte code was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time compiler.

Java程序占有少量的CPU資源,其字節(jié)碼能夠很容易被直接翻譯成機(jī)器代碼。

1.4.7 Distributed

Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols. In fact, accessing a resource using a URL is not much different from accessing a file. The original version of Java included features for intra-address-space messaging. This allowed objects on two different computers to execute procedures remotely. Java revived these interfaces in a package called Remote Method Invocation. This feature brings an unparalleled level of abstraction to client/server programming.

Java是一種能夠在Internet的分布式環(huán)境下運(yùn)行的語言,通過遠(yuǎn)程方法調(diào)用的接口包來實(shí)現(xiàn)遠(yuǎn)程訪問。

1.4.8 Dynamic

Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and expedient manner. This is crucial to the robustness of the applet environment, in which small fragments of byte code may be dynamically updated on a running system.

Java程序可以在運(yùn)行時(shí)動態(tài)地加載所需要的對象,這對于Applet小程序的運(yùn)行非常重要。

1.4.9 Security

The fundamental problem in the internet associated with Java is security. Java’s security model is one of the language’s key architectural features that make it an appropriate technology for networked environments. Security is important because networks provide a potential avenue of attack to any computer hooked to them. This concern becomes especially strong in an environment in which software is downloaded across the network and executed locally, as is done with Java applets, for example. Because the class files for an applet are automatically downloaded when a user goes to the containing Web page in a browser, it is likely that a user will encounter applets from untrusted sources. Without any security, this would be a convenient way to spread viruses. Thus, Java’s security mechanisms help make Java suitable for networks because they establish a needed trust in the safety of network-mobile code.

Java安全模型是Java語言體系結(jié)構(gòu)的關(guān)鍵性特性。它使得Java語言能夠適應(yīng)網(wǎng)絡(luò)環(huán)境。因?yàn)檫@種安全措施在網(wǎng)絡(luò)代碼上建立了一種信任感。

主站蜘蛛池模板: 博兴县| 夹江县| 泊头市| 眉山市| 遂宁市| 高陵县| 布尔津县| 常山县| 高碑店市| 两当县| 西充县| 肇源县| 平山县| 元朗区| 察雅县| 饶平县| 英吉沙县| 长武县| 沙雅县| 襄樊市| 台北县| 郴州市| 大名县| 柘荣县| 巧家县| 兴城市| 永福县| 右玉县| 将乐县| 宜川县| 同仁县| 雷山县| 庄河市| 枝江市| 喜德县| 曲麻莱县| 广丰县| 奉节县| 织金县| 昭觉县| 德保县|