What is Java?
Java is an important new programming language, designed by a team
at Sun Microsystems. Many of you
will have heard of it in the context of the Internet, which is where
it made its initial impact. Sun started developing Oak, the forerunner
of Java, in 1990. Java was released in 1993 and has grown from nothing
to become widely accepted as one of the world's most important
programming languages. All major web browsers now support Java and
companies such as Microsoft
and Apple are already building
support into their operating systems. Many of the new Network
Computers will run little other than Java programs.
At present, Java is usually thought of as a means of embedding fancy
graphics or interaction into web pages. However, it is capable of much
more than this. In fact, it is a language comparable to any other:
Fortran, C, C++, Pascal or Modula-3. It provides all of the standard
language constructs that you would expect and a standard library which
goes beyond what any of these provides. It is available on most
systems: Windows, Macintosh, Solaris,
Linux and many others, including
almost all varieties of UNIX.
As you will see later in this book, Java is capable of matrix
manipulation, RSA encryption, computing pi and all of the other types
of computation that a scientific programmer might want to perform.
Why use Java?
If you already know another language or are trying to decide which
to learn then you are probably thinking something like `OK, so Java
can do everything that more traditional languages do, but why should I
choose it in particular?'
From the point of view of the scientific programmer, Java has
several advantages:
- Java is totally platform independent. Your code will compile
literally unchanged on any system. In addition the compiled code
itself will run on any system. Even languages such as Pascal or C do
not provide this degree of portability; in these languages the amount
of accuracy that numerical data types store varies and certain pieces
of behaviour do not have a defined standard. You will be able to
switch systems or share code with colleagues much more easily.
- The Java standard libraries provide support for graphics. This is
not true of any other popular language. It means that even graphical
code is portable between systems, whereas under other systems it
usually needs complete rewriting.
- The Java standard libraries provide arbitrary precision arithmetic
in both integers and decimals. This is not true for any of the
languages that we have mentioned.
- Java is a modern language. Its many technical virtues range from
being object oriented to being multi-threaded, from being dynamic to
being architecture neutral and cover many other buzzwords. This may
not mean much to you; in practice it means that Java provides features
that are much more powerful than older languages such as Pascal or C,
and, since it was designed as a coherent whole, there are fewer
strange side effects than in C++. As a simple example, in any of
these three languages, if you allocate memory then you must keep
track of it and deallocate it. In Java the memory is automatically
tracked and deallocated when you have finished with it. Features like
this make it harder to write buggy code.
- Java code run over the Internet is secure. Java is designed so
that programs embedded in web pages can only have limited access to
your computer. For example, they cannot read or write to the hard disk
and cannot access memory allocated to different programs. When running
Java code inside a web browser, you are protected from any malicious
actions that it may try to perform. The only other technology for
embedding programs into web pages, Microsoft's ActiveX, makes no
attempt to provide any security at all. This security and its platform
independence makes Java the standard for programs on the web.
Simultaneously, the need for security does not cripple the Java
language: programs that you have explicitly downloaded onto your local
system or are writing yourself are as powerful (and potentially as
risky) as any other form of program.
- Java is free. This is almost true of C/C++ where any UNIX machine
will come with a compiler. However, at present Sun are providing their
JDK (Java Development Kit) free of charge for Window 98/NT and Solaris
and free ports exist to many other platforms. The website accompanying
this book, at
http://www.jscieng.co.uk/,
provides pointers to Sun's versions
for Windows and Solaris as well as to third-party ports of this
software to other platforms. Of course, other companies such as
SuperCede,
Borland,
Metrowerks,
Microsoft and
Symantec have also written
their own competing compilers, some with nicer graphical user
interfaces. You can pay and get a fuller service if you want to, but
the JDK defines the standard.
- With most of the computer industry backing it, we can expect
further exciting developments over the next few years.
In addition, if you are involved in any kind of teaching then Java
provides two further benefits.
- Java code can be run in a web browser over the Internet, allowing
you to write demonstration programs which your students will be able
to run at any connected computer.
- From a student's point of view, knowledge of Java is a very
marketable skill. Many courses include computer projects, usually
performed in a language such as Pascal. By doing such projects in
Java, the student loses nothing, since the basic syntax is as easy to
learn, but gains all of the advantages listed above, such as better
standard library support. In addition, the student gains experience
with a language which is actually used in the computer industry.
Why Buy this Book?
Java has made such an impact on the programming community that
bookshops are currently flooded with books on the subject. The vast
majority of these cover exactly the same ground. They are designed for
corporate programmers: people who intend to write flashy graphical
additions for web pages, standard business applications or networking
software. As such, these books discuss the entire standard library,
concentrating on issues such as graphics, user interface design and
networking. In addition, these books tend to assume quite a high level
of expertise and most of the popular titles, such as Java in a
Nutshell (Flanagan, 1997), assume knowledge of C/C++ and
familiarity with object orientation.
This book is different. It is aimed at the scientific programmer
and at the increasing number of science students who are being taught
Java as their first language. As such, the focus is different and
discussion of issues such as user interface components is almost
entirely omitted. Indeed, this book makes no attempt to cover the
entire standard library. Instead we concentrate on those parts which
will be of use to you. For example, the library supports arbitrary
precision arithmetic. Most other books would relegate this to an
aside. Here, however, this is the second part of the standard library
that we discuss (following on from the standard mathematical
functions), and we follow our discussion by implementing RSA (large
prime number) encryption.
In addition, unlike other Java books, our examples perform the
kind of computations that a scientific programmer will be interested
in. This book is not intended as a systematic source of good numerical
algorithms - the programs are intended to illustrate language
features. However, all of the examples perform a scientific
computation of some sort and there are brief introductions to more
serious numerical algorithms, software engineering and physical
modelling in the second part of the book. These introductions are
directed towards students who have not encountered these subjects
before. We also include a description of
Visual Numerics'
`JNL, a Numerical Library for Java'
and a simple library of our own,
`JSGL, a Scientific Graphics Library for
Java', to handle graphical output for you so that you do not need
to learn the full structure of Java graphics. Both of these libraries
are intended for professional numerical work. The JSGL will run on all
platforms that support Java, in contrast to most other graphical
systems and languages in which you must rewrite your code to switch
platforms.
This book has a fast track for those who already know C/C++, which
are superficially similar, and also a more elementary introduction
for students who have done no previous computing. Whichever category
you are in, I have tried to provide an easier introduction to the
subject than many other books on Java. No knowledge of any particular
programming language is assumed, nor is any familiarity with object
orientation.
Acknowledgements
I should like to thank Professor Brian Davies, who acted as my
scientific advisor, ensuring that the style did not get too technical
and suggesting some of the examples. I should also like to thank
Daniel Andor, Alan Bain, Tobias Berger, Sarah Clelland, Andrew
Kanaber, Tom Oinn, Andrew Serjantov and Chris Webb, students at
Cambridge University who acted as my proof-readers and Drs Tim Cutts,
Andrew Gee and Robert Hunt, members of staff with whom I had useful
conversations. I must stress, however, that it is of course the
author alone who is responsible for any errors in the final draft.
Richard J. Davies
Cambridge, England
August 1998