To save content items to your account,
please confirm that you agree to abide by our usage policies.
If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account.
Find out more about saving content to .
To save content items to your Kindle, first ensure no-reply@cambridge.org
is added to your Approved Personal Document E-mail List under your Personal Document Settings
on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part
of your Kindle email address below.
Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations.
‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi.
‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Any language should come with a well-stocked toolbox of utilities to ensure programming efficiency and convenience. The Java core language, in fact, includes a large package named, quite sensibly, java.util that holds classes to handle arrays, hash tables, time keeping, and other common tasks. In this chapter, we focus mostly on this package but also discuss some handy tools in other packages such as String and StringBuffer in java.lang and StringBuilder and enum, that were added to java.lang for J2SE 5.0 [1, 2]. We also discuss the Collections Framework and the new Generics feature of J2SE 5.0.
We also look at classes and techniques for handling numbers at two extremes. For bits we have the java.util.BitSet class. For arbitrarily large numbers and for those that with very long decimal fractions we have the java.math. BigInteger and java.math.BigDecimal classes.
We can only briefly describe here the general workings of these classes, many of which contain a great number of methods. See the class descriptions in the Java 2 API Specifications for the full details.
The java.util Package
The package java.util has been part of the core language since Java 1.0. However, several classes have been added in subsequent versions. As the name suggests, the classes in this package serve a number of useful utility purposes, and we discuss several of them in this chapter.
Chapters 16 and 17 described the client/server or distributed object paradigm at a somewhat abstract level using the UML notation language. Because of the abstract nature of that discussion there was almost no Java code in either of those chapters. In fact, the only place that any code snippets appeared at all was to illustrate UML concepts in a familiar concrete Java environment.
Now we will begin to explain how to really implement the distributed object paradigm using Java Remote Method Invocation (RMI). By the end of this chapter, we will have some real code that demonstrates simple communication between two distributed Java objects, though we still won't have a running application using the architecture of Chapters 16 and 17. That complete application will be developed in Chapter 20.
Before getting to the real code, we must first describe how distributed computing can be implemented. The concepts are not particularly new, nor are they unique to Java. However, just as in other areas, the Java platform offers advantages and an easy programming style that are absent in other languages. These advantages are discussed as we explain how distributed computing works. While the full capabilities of RMI are beyond the scope of this book, we will learn the basics of RMI, and with these skills we can implement the distributed architecture described in Chapters 16 and 17.
How distributed computing works
Consider a conventional (non-distributed) computer program, be it written in Java or some other language.
In Java the “class” is paramount. Essentially all coding resides within class definitions. Here and in the following chapters we develop the concepts and coding for classes and objects in Java.
For those new to object-oriented programming (OOP) the learning curve can be rather steep because several concepts must be understood before the overall picture comes into focus. The Web Course Supplements section for Chapter 3 offers additional introductory material to help get these concepts across.
Note that throughout the book we use the terms object and instance interchangeably.
Custom data types
In Chapter 2 we discussed Java primitive data types such as int, float, and boolean. Data of a given type means that memory is reserved for a value of that type and that only operations specific to that type can act upon the data. So, for example, a float value has 4 bytes of memory allocated for it with the sign, exponent, and significand bits arranged according to the representation discussed in Chapter 2. When an operation such as an addition or multiplication occurs upon a float value, the JVM executes floating-point operations that carry out the proper procedures for addition and multiplication with significands and exponents. For integer addition and multiplication the JVM executes a different set of operations unique to integer type data.
Threads in Java are processes that run in parallel within the Java Virtual Machine. When the JVM runs on a single real processor the parallelism is, of course, only apparent because of the high speed switching of threads in and out of the processor. Yet even in that case, threading can provide significant advantages. For example, while one thread deals with a relatively slow I/O operation such as downloading a file over the network, other threads can do useful work. Threads can assist in modularizing program design. An animation can assign different threads to rendering the graphics, to sound effects, and to user interactions so that all of these operations appear to take place simultaneously. Furthermore, a JVM can assign Java threads to native OS threads (but isn't required to) and on a multiprocessor system it could thus provide true parallel performance.
Java makes the creation and running of threads quite easy. We will concentrate on the basics of threading and only briefly touch on the subtle complications that arise when multiple threads interact with each other and need to access and modify common resources. Such situations can result in data race, deadlock, and other interference problems that result in distorted data or hung programs.
Introduction to threads
In Java you can create one or more threads within your program just as you can run one or more programs in an operating system [1–4].
In the previous chapter we introduced Java graphics and focused on the display of components, drawings, and images. Here we discuss how to build a graphical display that interacts with the user. A Graphical User Interface (GUI) requires dynamic components like buttons and menus that cause something to happen when the user activates them with the mouse or keyboard. These components generate messages called events that signal what action occurred. We will first look in this chapter at the underlying structure for generating and processing events.
We then introduce several more Swing components such as checkboxes and sliders and present demonstration programs that illustrate how to receive and process the events they generate. We discuss in more detail how components are arranged on the interface with the use of layout managers. For event handling and other tasks, we examine the use of inner classes and adapter classes. After a discussion of frames and menu bars, we demonstrate GUI construction and event techniques with a couple of programs involving histograms.
Events
The graphical user interface (GUI) offers a profoundly different programming environment than the old step-by-step, linear world of a procedural program communicating via a console. In the latter approach, the user simply starts a program and waits for it to churn through its algorithm and eventually reach the end and stop. In the GUI environment, the program instead waits for the user to select some task and then carries out that selected operation.
Java is a serious language suitable for demanding applications in science and engineering. Really, we promise! Java offers a lot more than just those little applets in your Web browser.
In JavaTech we focus on how Java can perform useful tasks in technical computing. These tasks might involve an animated simulation to demonstrate a scientific principle, a graphical user interface for an existing C or C++ computational engine, a distributed computing project, controlling and monitoring an experiment remotely via the Internet, or programming an embedded Java hardware processor in a device such as a remote sensor. While other Java books intended for the science and engineering audience concentrate primarily on numerical programming, we take a much broader approach and examine ways that Java can benefit programmers working on many different types of technical applications.
This project grew out of a course given by two of us (C.S.L. and Th.L.) at the Royal Institute of Technology in Stockholm, Sweden in which students of diverse backgrounds followed the class via the Internet. For this type of distance learning situation, we developed hypertext instructional material for delivery via the Web browser that allows for a high degree of self-study. This approach works especially well with Java since many of the demonstration programs run as applets within the browser.
This book provides a handy print companion to this hypertext course, which is available online at www.javatechbook.com.
CORBA is an acronym for Common Object Request Broker Architecture, a name that does not really convey to the new user the purpose of the technology. Most people just think of CORBA as the name of an important distributed object technology without really considering what the acronym stands for. For Java developers, RMI is generally the preferred distributed object architecture – especially when it is known that both client and server will be written in Java. However, CORBA has the advantage that it is language independent, meaning that non-Java clients can make CORBA calls to a CORBA server.
CORBA is a standard, so there is an official specification of that standard. The standard is maintained by the Object Management Group, a consortium of over 800 members. See the www.omg.org home page for voluminous information on CORBA and other technologies developed by the OMG. CORBA is also a very broad technology that covers much more than we introduce here. Our point is to demonstrate how CORBA technology can be used to implement straightforward distributed computing solutions analogous to the RMI example developed in Chapter 18.
A typical scientific application, such as the simulation described in Chapters 16 and 17, generally has only a few users. The developer writes both the client and server and, if developing in Java, probably uses RMI.
The term Java refers to more than just a computer language like C or Pascal. Java encompasses several distinct components:
A high-level language – Java is an object-oriented language whose source code at a glance looks very similar to C and C++ but is unique in many ways.
Java bytecode – A compiler transforms the Java language source code to files of binary instructions and data called bytecode that run in the Java Virtual Machine.
Java Virtual Machine (JVM) – A JVM program takes bytecode as input and interprets the instructions just as if it were a physical processor executing machine code. (We discuss actual hardware implementations of the Java interpreter in Chapter 24.)
Sun Microsystems owns the Java trademark (see the next section on the history of Java) and provides a set of programming tools and class libraries in bundles called Java Software Development Kits (SDKs). The tools include javac, which compiles Java source code into bytecode, and java, the executable program that creates a JVM that executes the bytecode. Sun provides SDKs for Windows, Linux, and Solaris. Other vendors provide SDKs for their own platforms (IBM AIX and Apple Mac OS X, for example). Sun also provides a runtime bundle with just the JVM and a few tools for users who want to run Java programs on their machines but have no intention of creating Java programs. This runtime bundle is called the Java Runtime Environment (JRE).
Our purpose in this chapter is to present the key concepts of quantum mechanics in the language of Hilbert spaces. The reader who has not previously met the physical ideas motivating quantum mechanics, and some of the more elementary applications of Schrödinger's equation, is encouraged to read any of a number of excellent texts on the subject such as [1–4]. Otherwise, the statements given here must to a large extent be taken on trust – not an altogether easy thing to do, since the basic assertions of quantum theory are frequently counterintuitive to anyone steeped in the classical view of physics. Quantum mechanics is frequently presented in the form of several postulates, as though it were an axiomatic system such as Euclidean geometry. As often presented, these postulates may not meet the standards of mathematical rigour required for a strictly logical set of axioms, so that little is gained by such an approach. We will do things a little more informally here. For those only interested in the mathematical aspects of quantum mechanics and the role of Hilbert space see [5–8].
Many of the standard applications, such as the hydrogen atom, will be omitted here as they can be found in all standard textbooks, and we leave aside the enormous topic of measurement theory and interpretations of quantum mechanics. This is not to say that we need be totally comfortable with quantum theory as it stands. Undoubtedly, there are some philosophically disquieting features in the theory, often expressed in the form of socalled paradoxes.