CHAPTER 4 Architecture Neutral, Portable,and Robust
第四章 体系结构中立、可移植和健壮的
With the phenomenal growth of networks, today's developers must "think distributed". Applications--even parts of applications--must be able to migrate easily to a wide variety of computer systems, a wide variety of hardware architectures, and a wide variety of operating system architectures. They must operate with a plethora of graphical user interfaces.
随着网络的显著增长,今天的开发人员必须“考虑分布式”。应用程序——甚至是应用程序的一部分——必须能够轻松地迁移到各种计算机系统、各种硬件体系结构和各种操作系统体系结构。它们必须使用过多的图形用户界面进行操作。
Clearly, applications must be able to execute anywhere on the network without prior knowledge of the target hardware and software platform. If application developers are forced to develop for specific target platforms, the binary distribution problem quickly becomes unmanageable. Various and sundry methods have been employed to overcome the problem, such as creating "fat" binaries that adapt to the specific hardware architecture, but such methods are not only clumsy but are still geared to a specific operating system. To solve the binary-distribution problem, software applications and fragments of applications must be architecture neutral and portable.
显然,应用程序必须能够在不了解目标硬件和软件平台的情况下在网络上的任何地方执行。如果应用程序开发人员被迫针对特定的目标平台进行开发,那么二进制分布问题很快就会变得难以管理。已经使用了各种各样的方法来克服这个问题,例如创建适应特定硬件体系结构的“胖”二进制文件,但是这些方法不仅笨拙,而且仍然针对特定的操作系统。要解决二进制分布问题,软件应用程序和应用程序片段必须是体系结构中立和可移植的。
R eliability is also at a high premium in the distributed world. Code from anywhere on the network should work robustly with low probabilities of creating "crashes" in applications that import fragments of code.
在分布式世界中,可靠性也非常重要。来自网络上任何地方的代码应该能够稳定地工作,并且在导入代码片段的应用程序中创建“崩溃”的可能性很低。
This chapter describes the ways in which Java has addressed the issues of architecture neutrality, portability, and reliability.
本章描述了Java处理体系结构中立性、可移植性和可靠性问题的方法。
4.1 Architecture Neutral
体系结构中立
The solution that the Java system adopts to solve the binary-distribution problem is a "binary code format" that's independent of hardware architectures, operating system interfaces, and window systems. The format of this system-independent binary code is architecture neutral. If the Java run-time platform is made available for a given hardware and software environment, an application written in Java can then execute in that environment without the need to perform any special porting work for that application.
Java系统用于解决二进制分布问题的解决方案是一种“二进制代码格式”,它独立于硬件体系结构、操作系统接口和窗口系统。这种与系统无关的二进制代码的格式是体系结构中立的。如果Java运行时平台可用于给定的硬件和软件环境,那么用Java编写的应用程序可以在该环境中执行,而不需要为该应用程序执行任何特殊的移植工作。
4.1.1 Byte Codes
字节码
The Java compiler doesn't generate "machine code" in the sense of native hardware instructions--rather, it generates bytecodes: a high-level, machine-independent code for a hypothetical machine that is implemented by the Java interpreter and run-time system.
Java编译器不会生成本机硬件指令意义上的“机器码”,而是生成字节码:一种高级的、与机器无关的代码,用于由Java解释器和运行时系统实现的假想机器。
One of the early examples of the bytecode approach was the UCSD P-System, which was ported to a variety of eight-bit architectures in the middle 1970s and early 1980s and enjoyed widespread popularity during the heyday of eight-bit machines. Coming up to the present day, current architectures have the power to support the bytecode approach for distributed software. Java bytecodes are designed to be easy to interpret on any machine, or to dynamically translate into native machine code if required by performance demands.
字节码方法的早期示例之一是UCSD P-System,它在20世纪70年代中期和80年代早期被移植到各种八位架构上,并在八位机器的全盛时期得到了广泛的普及。到目前为止,当前的架构能够支持分布式软件的字节码方法。Java字节码被设计为易于在任何机器上解释,或者根据性能需求动态地转换为本机代码。
The architecture neutral approach is useful not only for network-based applications, but also for single-system software distribution. In today's software market, application developers have to produce versions of their applications that are compatible with the IBM PC, Apple Macintosh, and fifty-seven flavors of workstation and operating system architectures in the fragmented UNIX® marketplace.
体系结构中立的方法不仅适用于基于网络的应用程序,而且适用于单系统软件分发。在当今的软件市场中,应用程序开发人员必须开发出与IBM PC、Apple Macintosh以及分散的UNIX®市场中的57种工作站和操作系统体系结构兼容的应用程序版本。
With the PC market (through Windows 95 and Windows NT) diversifying onto many CPU architectures, and Apple moving full steam from the 68000 to the PowerPC, production of software to run on all platforms becomes almost impossible until now. Using Java, coupled with the Abstract Window Toolkit, the same version of your application can run on all platforms.
随着PC市场(通过Windows 95和Windows NT)向许多CPU架构多样化,以及苹果从68000转向PowerPC,在此之前,在所有平台上运行软件的生产几乎是不可能的。使用Java和抽象窗口工具包,应用程序的相同版本可以在所有平台上运行。
4.2 Portable
可移植
The primary benefit of the interpreted byte code approach is that compiled Java language programs are portable to any system on which the Java interpreter and run-time system have been implemented.
解释字节码方法的主要好处是,编译后的Java语言程序可移植到Java解释器和运行时系统已经实现的任何系统上。
The architecture-neutral aspect discussed above is one major step towards being portable, but there's more to it than that. C and C++ both suffer from the defect of designating many fundamental data types as "implementation dependent". Programmers labor to ensure that programs are portable across architectures by programming to a lowest common denominator.
上面讨论的与体系结构无关的方面是实现可移植性的一个主要步骤,但它还有更多的内容。C和c++都存在将许多基本数据类型指定为“依赖于实现”的缺陷。程序员通过使用最低公分母来确保程序在架构之间的可移植性。
Java eliminates this issue by defining standard behavior that will apply to the data types across all platforms. Java specifies the sizes of all its primitive data types and the behavior of arithmetic on them. Here are the data types:
Java通过定义适用于所有平台上的数据类型的标准行为来消除这个问题。Java指定了它的所有基本数据类型的大小和它们的算术行为。以下是数据类型:
byte
8-bit two's complement,8位二进制补码
short
16-bit two's complement,16位二进制补码
int
32-bit two's complement,32位二进制补码
long
64-bit two's complement,64位二进制补码
float
32-bit IEEE 754 floating point,32位的IEEE 754浮点数
double
64-bit IEEE 754 floating point,64位的IEEE 754浮点数
char
16-bit Unicode character,16位Unicode字符
The data types and sizes described above are standard across all implementations of Java. These choices are reasonable given current microprocessor architectures because essentially all central processor architectures in use today share these characteristics. That is, most modern processors can support two's-complement arithmetic in 8-bit to 64-bit integer formats, and most modern processors support single- and double-precision floating point.
上面描述的数据类型和大小是所有Java实现的标准。考虑到当前的微处理器体系结构,这些选择是合理的,因为现在使用的所有中央处理器体系结构基本上都具有这些特征。也就是说,大多数现代处理器可以支持8位到64位整数格式的双补数运算,而且大多数现代处理器支持单精度浮点数和双精度浮点数。
The Java environment itself is readily portable to new architectures and operating systems. The Java compiler is written in Java. The Java run-time system is written in ANSI C with a clean portability boundary which is essentially POSIX-compliant. There are no "implementation-dependent" notes in the Java language specification.
Java环境本身很容易移植到新的体系结构和操作系统。Java编译器是用Java编写的。Java运行时系统是用ANSI C编写的,具有干净的可移植性边界,本质上是与posix兼容的。在Java语言规范中没有“依赖于实现”的注释。
4.3 Robust
健壮的
Java is intended for developing software that must be robust, highly reliable, and secure, in a variety of ways. There's strong emphasis on early checking for possible problems, as well as later dynamic (run-time) checking, to eliminate error-prone situations.
Java旨在以各种方式开发健壮、高度可靠和安全的软件。它非常强调对可能的问题的早期检查,以及稍后的动态(运行时)检查,以消除容易出错的情况。
4.3.1 Strict Compile-Time and Run-Time Checking
严格的编译时和运行时检查
The Java compiler employs extensive and stringent compile-time checking so that syntax-related errors can be detected early, before a program is deployed.
Java编译器采用广泛而严格的编译时检查,以便在部署程序之前及早发现与语法相关的错误。
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 its compile-time checking from C. Unfortunately, C++ and C are relatively lax, most notably in the area of method or function declarations. Java imposes much more stringent requirements on the developer: Java requires explicit declarations and does not support C-style implicit declarations.
强类型语言(如c++)的优点之一是它允许大量的编译时检查,因此可以尽早发现bug。不幸的是,c++继承了C语言在编译时检查方面的许多漏洞。不幸的是,c++和C语言相对宽松,特别是在方法或函数声明方面。Java对开发人员的要求要严格得多:Java需要显式声明,不支持c风格的隐式声明。
Many of the stringent compile-time checks at the Java compiler level are carried over to the run time, both to check consistency at run time, and to provide greater flexibility. The linker understands the type system and repeats many of the type checks done by the compiler, to guard against version mismatch problems.
Java编译器级别上的许多严格的编译时检查都转移到运行时,以便在运行时检查一致性,并提供更大的灵活性。链接器理解类型系统并重复编译器执行的许多类型检查,以防止版本不匹配问题。
The single biggest difference between Java and C or C++ is that Java's memory model eliminates the possibility of overwriting memory and corrupting data. Instead of pointer arithmetic, Java has true arrays and strings, which means that the interpreter can check array and string indexes. In addition, a programmer can't write code that turns an arbitrary integer into an object reference by casting.
Java与C或c++之间最大的区别是,Java的内存模型消除了覆盖内存和破坏数据的可能性。与指针算法不同,Java有真正的数组和字符串,这意味着解释器可以检查数组和字符串索引。此外,程序员不能编写通过强制转换将任意整数转换为对象引用的代码。
While Java doesn't pretend to completely remove the software quality assurance problem, removal of entire classes of programming errors considerably eases the job of testing and quality assurance.
虽然Java并没有假装完全消除软件质量保证问题,但是消除整个类的编程错误大大简化了测试和质量保证的工作。
4.4 Summary
Java--an architecture-neutral and portable programming language--provides an attractive and simple solution to the problem of distributing your applications across heterogeneous network-based computing platforms. In addition, the simplicity and robustness of the underlying Java language results in higher quality, reliable applications in which users can have a high level of confidence. The next chapter contains a brief discussion of Java's interpreted implementation.
Java是一种与体系结构无关的可移植编程语言,它为跨异构的基于网络的计算平台分发应用程序的问题提供了一个有吸引力的简单解决方案。此外,底层Java语言的简单性和健壮性带来了高质量、可靠的应用程序,用户可以对这些应用程序有很高的信心。下一章将简要讨论Java的解释实现。
好好学习,天天向上!继续下一章...
快速导航:
《Oracle官方Java白皮书译读》概览
《Oracle官方Java白皮书译读》之01:Java技术简介
《Oracle官方Java白皮书译读》之02:简单和友好的
《Oracle官方Java白皮书译读》之03:面向对象
《Oracle官方Java白皮书译读》之04:体系结构中立、可移植和健壮的
《Oracle官方Java白皮书译读》之05:解释执行和动态的
《Oracle官方Java白皮书译读》之06:安全性
《Oracle官方Java白皮书译读》之07:多线程
《Oracle官方Java白皮书译读》之08:性能比较
《Oracle官方Java白皮书译读》之09:基本系统和库
《Oracle官方Java白皮书译读》之10:HotJava万维网浏览器
《Oracle官方Java白皮书译读》之11:延伸阅读