码家

Web Platform, Cloud and Mobile Application Development

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

AN INTRODUCTION
TO JAVA

  

▼ JAVA  AS  A  PROGRAMMING  PLATFORM
▼ THE  JAVA  “WHITE PAPER”  BUZZWORDS
▼ JAVA  APPLETS  AND  THE  INTERNET
▼ A  SHORT  HISTORY  OF  JAVA
▼ COMMON  MISCONCEPTIONS  ABOUT  JAVA 

 

The first release of Java in 1996 generated an incredible amount of excitement, not  just in the computer press, but in mainstream media such as The New York Times, The  Washington Post, and Business Week. Java has the distinction of being the first and only  programming language that had a ten-minute story on National Public Radio. A  $100,000,000 venture capital fund was set up solely for products produced by use of a  specific computer language. It is rather amusing to revisit those heady times, and we  give you a brief history of Java in this chapter.

1996年Java第一次发布就引起了人们的极大兴趣。关注Java的人士不仅限于计算机出版界,还有诸如《纽约时报》、《华盛顿邮报》、《商业周刊》这样的主流媒体。Java是第一种也是唯一的一种在National Public Radio上占用了10分钟时间进行介绍的程序设计语言,并且还得到了$100 000 000的风险投资基金。这些基金全部用来支持用这种特别的的计算机语言开发的产品。重温那些令人兴奋的日子是很有意思的。本章将简要地介绍一下Java语言的发展历史。


Java As a Programming Platform
In the first edition of this book, we had this to write about Java:“As a computer language, Java’s hype is overdone: Java is certainly a good programming  language. There is no doubt that it is one of the better languages available to serious programmers. We think it could potentially have been a great programming  language, but it is probably too late for that. Once a language is out in the field, the ugly reality of  compatibility with existing code sets in.”

本书的第一版是这样描述Java的:“作为一种计算机语言,Java的广告词确实有点夸大其词。然而,Java的确是一种优秀的程序设计语言。作为一个名副其实的程序设计人员,使用Java无疑是一个好的选择。有人认为:Java将有望成为一种最优秀的程序设计语言,但还需要一个相当长的发展时期。一旦一种语言应用于某个领域,与现存代码的相容性问题就摆在了人们的面前。”
Our editor got a lot of flack for this paragraph from someone very high up at Sun Microsystems  who shall remain unnamed. But, in hindsight, our prognosis seems accurate.Java has a lot of nice language features—we examine them in detail later in this chapter.It has its share of warts, and newer additions to the language are not as elegant as the   original ones because of the ugly reality of compatibility.

我们的编辑手中有很多这样的广告词。这是Sun公司高层的某位不愿透露姓名的人士提供的。然后,现在看起来,当初的这些预测还是有一定的准确性的。Java有许多非常优秀的语言特性,本章稍后将会详细地讨论这些特性。由于相容性这个严峻的问题确实存在于现实中,所以,或多或少地还是有一些“累赘”被加到语言中,这就导致Java并不如想象中的那么完美无瑕。
But, as we already said in the first edition, Java was never just a language. There are lots  of programming languages out there, and few of them make much of a splash. Java is a  whole platform, with a huge library, containing lots of reusable code, and an execution  environment that provides services such as security, portability across operating systems, and automatic garbage collection.

但是,正像我们在第一版中已经指出的那样,Java并不只是一种语言。在此之前出现的那么多语言也没有能够引起那么大的轰动。Java是一个完整的平台,有一个庞大的库,其中包含很多可重用的代码和一个提供诸如安全性、跨操作系统的可移植性以及自动垃圾收集等服务的执行环境。

As a programmer, you will want a language with a pleasant syntax and comprehensible  semantics (i.e., not C++). Java fits the bill, as do dozens of other fine languages. Some  languages give you portability, garbage collection, and the like, but they don’t have  much of a library, forcing you to roll your own if you want fancy graphics or networking  or database access. Well, Java has everything—a good language, a high-quality  execution   environment, and a vast library. That combination is what makes Java an  irresistible proposition to so many programmers.

作为一名程序设计人员,常常希望能够有一种语言,它具有令人赏心悦目的语法和易于理解的语义(C++不是这样的)。与许多其他的优秀语言一样,Java恰恰满足了这些要求。有些语言提供了可移植性、垃圾收集器等等,但是,没有提供一个大型的库。如果想要有奇特的绘图功能,网络连接功能和数据库存取功能就必须自己动手编写代码。Java这种功能齐全的出色语言,具有高质量的执行环境以及庞大的库。正是因为它集多种优势于一身,所以对广大的程序设计人员有着不可抗拒的吸引力。

The Java “White Paper” Buzzwords
The authors of Java have written an influential White Paper that explains their design  goals and accomplishments. They also published a shorter summary that is organized  along the following 11  buzzwords:
Simple             Portable
Object Oriented         Interpreted
Network-Savvy          High Performance
Robust              Multithreaded
Secure             Dynamic
Architecture Neutral

Java的设计者已经编写了颇有影响里的白皮书,用来解释设计的初衷以及完成的情况,并且发布了一个简短的摘要。这个摘要用下面的11个关键术语进行组织:

简单性              可移植性

面向对象             解释型

网络技能(Network-Savvy)    高性能

健壮性              多线程

安全性              动态性

体系结构中立

In this section, we will
• Summarize, with excerpts from the White Paper, what the Java designers say about
each buzzword; and
• Tell you what we think of each buzzword, based on our experiences with the current  version of Java.

本节将论述下列主要内容:

  • 给出白皮书中对每个关键术语的概述,这是Java设计者对相关术语的论述
  • 凭借Java当前版本的使用经验,给出对这些术语的理解。

NOTE: As we write this, the White Paper can be found at http://java.sun.com/docs/white/langenv/. The summary with the 11 buzzwords is at http://java.sun.com/docs/overviews/java/java-overview-1.html.

注释:白皮书可以在http://java.sun.com/docs/white/langenv/上找到。对于11个关键术语的论述请参看http://java.sun.com/docs/overviews/java/java-overview-1.html.

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. 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.

1.2.1简单性

人们希望构建一个无需深奥的专业训练就可以进行编程的系统,并且要符合当今的标准惯例。因此,尽管人们发现C++不太适用,但是在设计Java的时候还是尽可能地接近C++,以便系统更易于理解。Java剔除了C++中许多很少使用、难以理解、易混淆的特性。在目前看来,这些特性带来的麻烦远远多于其带来的好处。

The syntax for Java is, indeed, a cleaned-up version of the syntax for C++. There is no  need for header files, pointer arithmetic (or even a pointer syntax), structures, unions, operator overloading, virtual base classes, and so on. (See the C++ notes interspersed  throughout the text for more on the differences between Java and C++.) The designers  did not, however, attempt to fix all of the clumsy features of C++. For example, the syntax  of the switch statement is unchanged in Java. If you know C++, you will find the transition  to the Java syntax easy.

的确,Java语法是C++语法的一个“纯净”版本。这里没有头文件、指针运算(甚至指针语法)、结构、联合、操作符重载、虚基类等等(请参阅本书各个章节给出的C++注释,那里比较详细地解释了Java与C++之间的区别)。然而,设计者并没有试图清除C++中所有不适当的特性。例如,switch语句的语法在Java中就没有改变。如果知道C++就会发现可以轻而易举地将其转换成Java。

If you are used to a visual programming environment (such as Visual Basic), you will not find Java simple. There is much strange syntax (though it does not take long to get the hang of it). More important, you must do a lot more programming in Java. The beauty of Visual Basic is that its visual design environment almost automatically provides a lot of the infrastructure for an application. The equivalent functionality must be programmed manually, usually with a fair bit of code, in Java. There are, however,third-party development environments that provide “drag-and-drop”-style program development.

如果已经习惯于使用可视化的编程环境(例如Visual Basic),你就不会觉得Java简单了。Java有许多奇怪的语法(尽管掌握其要领并不需要很长时间),更重要的是,使用Java需要自己编写大量的程序。Visual Basic的魅力在于它的可视化设计环境几乎自动地为应用程序提供了大量的基础结构。而使用Java实现同样的功能却需要手工地编制代码,通常代码量还相当大。然而,已经有一些支持“拖放”风格程序开发的第三方开发环境。

Another aspect of being simple is being small. One of the goals of Java is to enable the construction of software that can run stand-alone in small machines. The size of the basic interpreter and class support is about 40K bytes; adding the basic standard libraries and thread support (essentially a self-contained microkernel) adds an
additional 175K.

简单的另一个方面是小。Java的目标之一是支持开发能够在小型机器上独立运行的软件。基本的解释器以及类支持大约仅为40KB;再加上基础的标准类库和对线程的支持(基本上是一个自包含的微内核)大约需要增加175KB。

This was a great achievement at the time. Of course, the library has since grown to huge  proportions. There is now a separate Java Micro Edition with a smaller library, suitable  for embedded devices.

在当时,这是一个了不起的成就。当然,由于不断的发展,类库已经相当庞大了。现在一个独立的具有较小类库的Java微型版(Java Micro Edition)用于嵌入式设备。

Object Oriented
Simply stated, object-oriented design is a technique for programming that focuses  on the data (= objects) and on the interfaces to that object. To make an analogy with  carpentry, an “object-oriented” carpenter would be mostly concerned with the chair he was building, and secondarily with the tools used to make it; a “non-objectoriented”  carpenter would think primarily of his tools. The object-oriented facilities  of Java are essentially those of C++.

1.2.2面向对象

简单地讲,面向对象设计师一种程序设计技术。它将重点放在数据(即对象)和对象的接口上。用木匠打一个比方,一个“面向对象的”木匠始终关注的是所制作的椅子,第二位才是所使用的工具;一个“非面向对象的”木匠首先考虑的是所用的工具。在本质上,Java的面向对象能力与C++是一样的。

Object orientation has proven its worth in the last 30 years, and it is inconceivable that a  modern programming language would not use it. Indeed, the object-oriented features of  Java are comparable to those of C++. The major difference between Java and C++ lies in  multiple inheritance, which Java has replaced with the simpler concept of interfaces, and in the Java metaclass model (which we discuss in Chapter 5).

在过去的30年里,面向对象已经证明了自身的价值,一种现代的程序设计语言不适用面向对象技术简直让人难以置信。的确,Java的面向对象特性与C++旗鼓相当。Java与C++的主要不同点在于多继承,在Java中,取而代之的是简单的接口概念,以及Java的元类(metaclass)模型(有关这部分内容将在第5章中讨论)。

NOTE: If you have no experience with object-oriented programming languages, you will
want to carefully read Chapters 4 through 6. These chapters explain what object-oriented
programming is and why it is more useful for programming sophisticated projects than are
traditional, procedure-oriented languages like C or Basic.

如果没有使用面向对象程序设计语言的经验,你一定要仔细阅读第4章~第6章。这些章节解释了什么是面向对象程序设计以及在编程实现复杂的项目时为什么比传统的像C或Basic这样的面向过程的语言更加有效。

Network-Savvy
Java has an extensive library of routines for coping with TCP/IP protocols like  HTTP and FTP. Java applications can open and access objects across the Net via  URLs with the same ease as when accessing a local file system.

1.2.3  网络技能

Java有一个扩展的例程序,用于处理像HTTP和FTP这类的TCP/IP协议。Java应用程序能够通过URL打开和访问网络上的对象,其便捷程度就好像访问本地文件一样。

We have found the networking capabilities of Java to be both strong and easy to use.Anyone who has tried to do Internet programming using another language will revel in  how simple Java makes onerous tasks like opening a socket connection. (We cover networking  in Volume II of this book.) The remote method invocation mechanism enables   communication between distributed objects (also covered in Volume II).

人们已经看到Java的网络能力强大且易于使用。任何曾经试图使用其他语言进行网络编程的人都会惊呼Java竟然把类似打开socket链接这类繁重的任务都变得如此简单(在本书的卷II中介绍网络链接)。另外,远程方法调用机制使得分布式对象之间可以进行通信(也将在卷II中介绍)。

Robust
Java is intended for writing programs that must be reliable in a variety of ways.Java puts a lot of emphasis on early checking for possible problems, later dynamic  (runtime) checking, and eliminating situations that are error-prone. . . . 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.

1.2.4 健壮性

Java的设计目标之一在于使得Java编写的程序具有多方面的可靠性。Java投入了大量的精力进行早期的问题检测、后期动态的(运行时)检测,并消除了有出错倾向的状态。。。Java和C++最大的不同在于Java采用的指针模型可以消除重写内存和损坏数据的可能性。

This feature is also very useful. The Java compiler detects many problems that, in other  languages, would show up only at runtime. As for the second point, anyone who has  spent hours chasing memory corruption caused by a pointer bug will be very happy  with this feature of Java.

这个特性非常有用。Java编译器能够检测许多在其他语言中仅在运行时刻才能够检测出来的问题。至于第二点,对于曾经花费几个小时来检查由于指针bug而引起内存冲突的人来说,一定很喜欢Java的这一特性。

If you are coming from a language like Visual Basic that doesn’t explicitly use pointers, you are probably wondering why this is so important. C programmers are not so lucky.They need pointers to access strings, arrays, objects, and even files. In Visual Basic, you do not use pointers for any of these entities, nor do you need to worry about memory allocation for them. On the other hand, many data structures are difficult to implement in a pointerless language. Java gives you the best of both worlds. You do not need pointers
for everyday constructs like strings and arrays. You have the power of pointers if you need it, for example, for linked lists. And you always have complete safety, because you can never access a bad pointer, make memory allocation errors, or have to protect against memory leaking away.

如果曾经只使用过Visual Basic这类没有显式指针的语言,你会感觉这么说似乎有些小题大作了。然后,C程序员就没有这样幸运了。他们需要利用指针存取字符串、数组、对象、甚至文件。在Visual Basic中,根本不必使用指针访问这些实体,也不必关心有关内存分配的问题。另一方面,在没有指针的语言中,许多数据结构很难实现。Java具有双方的优势。它不需要使用指针构造诸如字符串、数组这样的结构。如果必要的话,它也能够具有指针的能力,如链表。Java绝对是安全的,其原因是永远不会存在一个“坏的”指针,造成内存非配的错误,也不必防范内存泄露。

Secure
Java is intended to be used in networked/distributed environments. Toward that end, a lot of emphasis has been placed on security. Java enables the construction of virus-free, tamper-free systems.

1.2.5 安全性

Java适用于网络/分布式环境。为了达到这个目标,在安全方面投入了很大精力。使用Java可以构建防病毒、防篡改的系统。

In the first edition of Core Java we said: “Well, one should ‘never say never again,’” and we turned out to be right. Not long after the first version of the Java Development Kit was shipped, a group of security experts at Princeton University found subtle bugs in the security features of Java 1.0. Sun Microsystems has encouraged research into Java security, making publicly available the specification and implementation of the virtual machine and the security libraries. They have fixed all known security bugs quickly. In any case, Java makes it extremely difficult to outwit its security mechanisms. The bugs found so far have been very technical and few in number.

本书的第1版曾经说过:“永远不要把话说绝!”。事实证明这是正确的。在Java开发工具箱第1版启用后不久,普林斯顿大学的一些安全专家们就发现了在JDK1.0中的某些安全特性方面存在着一些非常隐蔽的bug。Sun Microsystems大力支持对Java的安全性的研究,制定了供人们使用的规范,实现了虚拟机和安全库,并迅速地处理了所有已知的安全bug。在任何情况下,蒙骗Java的安全机制都是十分困难的。现在,发现bug的技术越来越强,数目越来越少。

From the beginning, Java was designed to make certain kinds of attacks impossible,among them:
• Overrunning the runtime stack—a common attack of worms and viruses
• Corrupting memory outside its own process space
• Reading or writing files without permission

从一开始,Java就设计成能够防范各种袭击,其中包括:

  • 运行时堆栈溢出。如,蠕虫等病毒常用的袭击手段。
  • 在自己的处理控件之外破外内存。
  • 未经授权读写文件。

A number of security features have been added to Java over time. Since version 1.1, Java has the notion of digitally signed classes (see Volume II). With a signed class, you can be sure who wrote it. Any time you trust the author of the class, the class can be allowed more privileges on your machine.

许多安全特性相继不断地加入到Java中。自从Java1.1问世以来,Java就有了数字签名类(digitally signed classes)的概念(请参看卷II)。通过数字签名类,可以确定类的作者。如果信任这个类的作者,这个类就可以在机器上拥有更多的权限。

NOTE: A competing code delivery mechanism from Microsoft based on its ActiveX technology relies on digital signatures alone for security. Clearly this is not sufficient—as any user of Microsoft’s own products can confirm, programs from well-known vendors do crash and create damage. Java has a far stronger security model than that of ActiveX because it controls the application as it runs and stops it from wreaking havoc.

注释:来自微软的基于ActiveX技术的竞争代码传输机制,其安全性完全依赖于数字签名。这显然是不够的,因为微软自身产品的任何用户都可以证实,来自知名提供商的程序会崩溃并对系统产生危害。Java的安全机制比ActiveX要强得多,因为它是在应用程序运行时加以控制并制止恶意性破坏的。

Architecture Neutral
The compiler generates an architecture-neutral object file format—the compiled code is executable on many processors, given the presence of the Java runtime system. The Java compiler does this by generating bytecode instructions which have nothing to do with a particular computer architecture. Rather, they are designed to be both easy to interpret on any machine and easily translated into native machine code on the fly.

1.2.6体系结构中立

编译器生成一个体系结构中立的目标文件格式,这是一种编译过的代码,只要有Java运行时系统,就可以在许多处理器上运行。Java编译器通过生成与特定的计算机体系结构无关的字节码指令来实现这一特性。精心设计的字节码不仅可以很容易地再任何机器上解释执行,而且还可以迅速地翻译成本地机器的代码。

This is not a new idea. More than 30 years ago, both Niklaus Wirth’s original implementation  of Pascal and the UCSD Pascal system used the same technique.

这并不是什么新的思路。30多年前,Niklaus Wirth实现的原始Pascal以及UCSD Pascal系统都使用了这种技术。

Of course, interpreting bytecodes is necessarily slower than running machine instructions at full speed, so it isn’t clear that this is even a good idea. However, virtual machines have the option of translating the most frequently executed bytecode sequences into machine code, a process called just-in-time compilation. This strategy has proven so effective that even Microsoft’s .NET platform relies on a virtual machine.

当然,解释字节码肯定会比全速地运行机器指令慢很多。所以说,这是不是一好的思路还很难讲!然而,虚拟机有一个选项,可以将使用最频繁的字节码序列翻译成机器码,这一过程被称为即时编译。这一策略已经证明十分有效,致使微软的.NET平台也依赖于虚拟机。

The virtual machine has other advantages. It increases security because the virtual machine can check the behavior of instruction sequences. Some programs even produce bytecodes on the fly, dynamically enhancing the capabilities of a running program.

虚拟机还有一些其他的优点。虚拟机可以检测指令序列的行为,以增强其安全性。有些程序还可以快速地生成字节码,并动态地增强所运行程序的处理能力。

Portable
Unlike C and C++, there are no “implementation-dependent” aspects of the specification.The sizes of the primitive data types are specified, as is the behavior of arithmetic
on them.

1.2.7 可移植性

与C和C++不同,Java规范中没有“依赖具体实现”的地方。基本数据类型的大小以及有关算法都做了明确的说明。

For example, an int in Java is always a 32-bit integer. In C/C++, int can mean a 16-bit integer, a 32-bit integer, or any other size that the compiler vendor likes. The only restriction is that the int type must have at least as many bytes as a short int and cannot have more bytes than a long int. Having a fixed size for number types eliminates a major porting headache. Binary data is stored and transmitted in a fixed format, eliminating confusion about byte ordering. Strings are saved in a standard Unicode format.

例如,Java中的int永远为32位的整数,而在C/C++中,int可能是16位整数、32位整数,也可能是编译器提供商指定的其他大小。惟一的限制只是int类型的大小不能低于short int,并且不能高于long int。在Java中,数据类型具有固定的大小,这消除了代码移植时令人头痛的主要问题。二进制数据以固定的格式进行的存储和传输,消除了字节顺序的困扰。字符串是用标准的Unicode格式存储的。

The libraries that are a part of the system define portable interfaces. For example, there is an abstract Window class and implementations of it for UNIX, Windows, and the Macintosh.

作为系统组成部分的类库,定义了可移植的接口。例如,有一个抽象的Window类给出了在UNIX、Windows和Machintosh环境下的不同实现。

As anyone who has ever tried knows, it is an effort of heroic proportions to write a program that looks good on Windows, the Macintosh, and ten flavors of UNIX. Java 1.0 made the heroic effort, delivering a simple toolkit that mapped common user interface elements to a number of platforms. Unfortunately, the result was a library that, with a lot of work, could give barely acceptable results on different systems. (And there were often different bugs on the different platform graphics implementations.) But it was a start. There are many applications in which portability is more important than user interface slickness, and these applications did benefit from early versions of Java. By now, the user interface toolkit has been completely rewritten so that it no longer relies on the host user interface. The result is far more consistent and, we think, more attractive than in earlier versions of Java.

凡是尝试过的人都知道,要编写一个在Windows、Macintosh和10种不同风格的、在UNIX上看起来都很不错的程序有多么困难。Java 1.0就尝试着做了这么一个壮举,发布了一个将常用的用户界面元素射到不同平台上的简单工具箱。遗憾的是,花费了大量的心血,却构建了一个在各个平台上都难以接受的库(而且,在不同平台的图形实现中有不同的bug)。不过,这毕竟是个开端。对于许多应用问题来说,可移植性比华而不实的用户界面更加重要;而且这些应用程序从Java的早期版本中获益匪浅。现在,用户界面工具箱已经完全重写了,不再依赖于主机的用户接口。现在的Java版本比早期版本更加稳定,更加吸引人。

Interpreted
The Java interpreter can execute Java bytecodes directly on any machine to which the interpreter has been ported. Since linking is a more incremental and lightweight process, the development process can be much more rapid and exploratory.

1.2.8  解释型

Java解释器可以在任何移植了解释器的机器上执行Java字节码。由于链接是一个增值且简便的过程,所以,开发过程也变得更加快捷,更加具有探索性。

Incremental linking has advantages, but its benefit for the development process is clearly overstated. Early Java development tools were, in fact, quite slow. Today, the bytecodes are translated into machine code by the just-in-time compiler.

增值链接有其优势,但给开发过程带来的好处显然是言过其实了。事实上,早期的Java开发工具的速度相当慢。现在,使用即时编译器将字节码翻译成机器码。

High Performance
While the performance of interpreted bytecodes is usually more than adequate,there are situations where higher performance is required. The bytecodes can be translated on the fly (at runtime) into machine code for the particular CPU the application is running on.

1.2.9 高性能

尽管对解释后的字节码性能已经比较满意,但在有些场合下却需要更加高效的性能。字节码可以(在运行时刻)快速地翻译成运行这个应用程序的特定CPU的机器码。

In the early years of Java, many users disagreed with the statement that the performance was “more than adequate.” Today, however, the just-in-time compilers have become so good that they are competitive with traditional compilers and, in some cases, even outperform them because they have more information available. For example, a just-in-time compiler can monitor which code is executed frequently and optimize just that code for speed. A more sophisticated optimization is the elimination (or “inlining”) of function calls. The just-in-time compiler knows which classes have been loaded. It can use inlining when, based upon the currently loaded collection of classes, a particular  function is never overridden, and it can undo that optimization later if necessary.

使用Java的头几年,许多用户不同意这样的看法:性能就是“适用性更强”。然而,现在的即时编译器已经非常出色了,以至于成为了传统编译器的竞争对手。在某些情况下,甚至超过了传统编译器,其原因是它们含有更多的可用信息。例如,即时编译器可以监控经常执行哪些代码并优化这些代码以提高速度。更为复杂的优化是消除函数调用(即“内嵌”)。即时编译器知道哪些类已经加载。如果基于当前加载的类集,且特定的函数不被覆盖的话就可以内嵌。必要时,还可以撤销优化。

Multithreaded
[The] benefits of multithreading are better interactive responsiveness and real-time  behavior.

1.2.10 多线程

多线程可以带来更好的交互响应和实时行为。

If you have ever tried to do multithreading in another language, you will be pleasantly  surprised at how easy it is in Java. Threads in Java also can take advantage of multiprocessor systems if the base operating system does so. On the downside, thread implementations on the major platforms differ widely, and Java makes no effort to be platform independent in this regard. Only the code for calling multithreading remains
the same across machines; Java offloads the implementation of multithreading to the  underlying operating system or a thread library. Nonetheless, the ease of multithreading  is one of the main reasons why Java is such an appealing language for server-side
development.

如果曾经使用过其他语言编写多线程的应用程序,就会对Java多线程处理的便捷性惊叹不已。只要操作系统支持,Java中的线程就可以利用多个处理器。在底层,主流平台的线程实现机制各不相同,Java并没有花费太大的力气对此实现平台无关性。在不同的机器上,只是调用多线程的代码完全相同;Java把多线程的实现交给了底层的操作系统或线程库来完成。尽管如此,多线程编译的简单性是Java成为颇具魅力的服务器端开发语言的主要原因之一。

Dynamic
In a number of ways, Java is a more dynamic language than C or C++. It was designed to adapt to an evolving environment. Libraries can freely add new methods and instance variables without any effect on their clients. In Java, finding out runtime type information is straightforward.

1.2.11  动态性

从各种角度看,Java与C或C++相比更加具有动态性。它能够适应不断发展的环境。库中可以自由地添加新方法和实例变量,而对客户端却没有任何影响。在Java中找出运行时类型信息十分简单。

This is an important feature in those situations in which code needs to be added to a running program. A prime example is code that is downloaded from the Internet to run in a browser. In Java 1.0, finding out runtime type information was anything but straightforward, but current versions of Java give the programmer full insight into both the structure and behavior of its objects. This is extremely useful for systems that need to analyze objects at runtime, such as Java GUI builders, smart debuggers, pluggable
components, and object databases.

当需要将某些代码添加到正在运行的程序中时,动态性将是一个非常重要的特性。一个很好的例子是:从Internet上下载代码,然后在浏览器上运行。Java 1.0 中,不能直接获得运行时的类型信息,而Java的当前版本允许程序员知道对象的结构和行为。这对于必须在运行时分析对象的系统来说非常有用。这些系统有:Java GUI构建器、只能调试器、可插入组件以及对象数据库。

NOTE: Shortly after the initial success of Java, Microsoft released a product called J++ with a programming language and virtual machine that was almost identical to Java. At this point, Microsoft is no longer supporting J++ and has instead introduced another language called C# that also has many similarities with Java but runs on a different virtual machine. There is even a J# for migrating J++ applications to the virtual machine used by C#. We do not cover J++, C#, or J# in this book.

注释:Java成功地推出后不久,微软就发布了一个叫做J++的产品,它与Java有相同的编程语言以及虚拟机。现在微软不再支持J++,取而代之的是另一种被称为C#的语言。C#与Java有很多相似之处,然后使用的却是不同的虚拟机。甚至还有一种J#语言可将J++的应用迁移到使用C#的虚拟机上。本书不准备介绍J++、C#或J#语言。

posted on 2011-05-23 16:23  海山  阅读(273)  评论(0编辑  收藏  举报