Language and Libraries

Let's spend a minute to review some software terms that are often taken for granted. If you are already familiar with this, just skip ahead until you reach something new. The terminology is spelled out in detail here to give a solid basis for the material that follows in the rest of the book.

What a language is

When people talk about a “programming language”, they mean things like:

  • How the language describes data,

  • The statements that work on data, and

  • The ways the two can be put together. The set of rules describing how you can put together programs in the language is called a grammar. The grammar for a programming language is written in mathematical language, and it defines how expressions are formed, what statements look like, and so on. We'll stay away from mathematics and explain things in English.

Java is a programming language in the same part of the languages family tree as C++, Pascal, or Basic. Java adopts ideas from non-mainstream languages like Smalltalk and Lisp, too. Java is a strongly typed language, meaning that the compiler strictly checks operations and will only allow those that are valid for that type of operand (you can't multiply two character strings, but you may append them). Java is object-oriented, meaning that the data declarations are very closely tied to the statements that operate on the data. Object-oriented means more than just tying data to statements, and we expand on that in chapters 2 and 5.

Statements are grouped into what other languages call functions, procedures, or subroutines. In an object-oriented language, we call the functions methods as they are the method or way to process some data. Methods can call other methods and can be recursive (call themselves). Program execution begins in a method with the special name main().Statements in Java look similar to statements in many other high-level languages. Here are some examples of Java statements:

y = y + 1;
if ( isLeapYear(y) )      // this is a comment
     febDays = 29;
else febDays = 28;

Although Java adds some new things, it is equally notable for what is left out. The designer of C++ once wrote, “Within C++, there is a much smaller and cleaner language struggling to get out,” (Bjarne Stroustrup, The Design and Evolution of C++, Addison-Wesley, 1994: 207). Of the people who agree with that prophetic statement, many think that the name of that smaller and cleaner language is Java.

Java has eight basic or primitive datatypes, such as integers, characters, and floating-point numbers. All data is represented as one of these eight built-in elementary types, or as an object or array made up of these types. The size of each type is laid down in the language and is the same on all platforms. Here are some other Java language features that will be covered in later chapters:

  • Threads. Threads let a single program do more than one thing at once, just as timesharing lets a computer system run more than one program at once.

  • Exceptions. Exceptions help a programmer deal with error conditions when most convenient, instead of cluttering up the main flow of control with lots of checking and error-handlingcode.

  • Garbage collection. With garbage collection, the run-time library, not the programmer, manages the use of dynamic storage and reclaims (frees) memory that is no longer in use in a program.

A programming language typically has a specification, which is a weighty document saying what the expressions, statements, and their various groupings actually mean. That is, what changes in memory or control flow will occur when an expression is evaluated or a statement is executed. This is termed the semantics of the language. The first half of this book, up to Chapter 15, explains the features and meaning (semantics) of the Java language. Don't worry—the chapters are short and well organized.

Sun has published a Java Language Specification (JLS). It is written mostly for an audience of compiler implementors, and is not intended as a tutorial for programmers (that's what this book is for). But it can be fun to dip into the JLS anyway when you want to play language lawyer or resolve a tricky question, and you can find it online at http://java.sun.com/docs/books/jls/index.html.

What a library is

Early in the history of programming, people realized that some routines were needed over and over again in different programs. It makes sense to move frequently reused code into a separate file or library that all programs can share. The kind of code that belongs in a library are things like code to open a file, or read in a block of bytes, or get the current timestamp from the operating system.

A library is not an executable program in its own right. It contains executable code that is brought into your program's address space, when your program calls a routine in the library. The work of finding the right library and loading its code into your process memory at run-time is called dynamic linking and it's an important feature of Java. “Dynamic” in compiler terminology just means “is done at run-time”, and contrasts with “static” meaning “done at compile time”.

The entire set of routines in a library (the routine names, what they can return, and the parameter types) is known as the Application Programming Interface, or API. Modern languages check that the calls to a library are consistent with the API of the library implementation. The checking needs to be done twice: at compile time to catch any mismatches as early as possible, and at run-time to confirm that nothing has changed. So the compiler and the run-time environment both need to know where to find the application libraries.

When working on large suites of programs, application programmers will frequently design and implement their own libraries (accumulation of re-usable routines). A retail sales application might have a library for a customer transaction, with all the operations you can do on a transaction: sale, return, void, or cancel. Any time a programmer wants code to process a transaction, he or she calls the appropriate library routine. If a bug is found in transaction code, it only needs to be fixed in one place (the library) and not in all the places where the library routine is called.

What the run-time library is

Just as application suites are supported by libraries, the compiler vendor provides a set of libraries collectively known as the run-time library. The run-time library or “the run-time” supports operations needed by programs in the course of execution.

The classic run-time library handles a small number of house-keeping tasks like memory management, maintaining a stack, and dynamic array support. Programmers don't call routines in a classic run-time library; the calls are planted automatically by the compiler when needed. Apart from the implicit calls, there is no significant difference between code in the run-time library and code in an application library. It's implemented the same way and has the same overall goals. Programmers can count on the presence of the run-time library, but because they don't have to tell the compiler where to find it and because they don't call it explicitly, they sometimes aren't really aware of its presence.

Java takes a radically different approach. Java has a very extensive run-time library—greater than any other programming language to date. In addition to the standard house-keeping operations in support of program execution, Java comes bundled with scores of packages (the Java term for a library) to carry out tasks useful to many applications. These libraries form part of the Java run-time library by definition, and you can absolutely depend on their standard presence in an implementation. Unlike a classic run-time library, the programmer will explicitly call routines in these Java packages when needed, and hence needs to know about the Java run-times and what they do.

Much of the value of Java is in this set of cross-platform APIs that come with the compiler system. Java has possibly the easiest-to-use networking and windowing (GUI) support of any language. For example, there is a single library function to read a JPEG file and display it as an icon in a GUI. There is another library function to make a standard connection, known as a socket, to any program (not just Java code) on another computer anywhere on the Internet. Java makes network software easy to build.

Some of the standard Java packages are shown in Table 1-1

Table 1-1. A few of the many Java libraries (packages) available to all programs

Package name

Purpose

java.lang

This provides the core support for language features, corresponding to a classic run-time library.

java.io

This package provides basic I/O to the file system

java.net

This library contains the code that lets you implement network applications.

java.util

This library contains data structures (e.g. a tree, a set, a map) and common operations on them. Also other miscellaneous utility classes (date/time support, random numbers, etc.).

java.util.regex

Nested in the java.util package, this library supports text searching using regular expressions patterns.

java.sql

This library provides tools for database access.

You can deduce that there is a hierarchy to package names: nested packages are shown by names separated by a period. E.g. there is a package called java.util full of utilities. One of its members is a package called java.util.regex, which has classes dealing with utilities for regular expressions. The hierarchy helps organize the hundreds of libraries, grouping related ones together. The package names shown in this table are exactly the names used in Java programs.

Once a language has been specified and implemented, it is quite a big deal to make further changes to the language. However, it is trivial to add new libraries because they don't affect any existing programs, they don't need compiler or interpreter changes, and they don't change any tools that process object code. Each new release of Java has added some pretty hefty new packages.

The Preface on page xix has a table showing how Java has evolved over a series of successive releases. Up until Java 1.5, there had only been a few new features added to the language. Java 1.5 introduces a major new language feature called "generics" which is explained in Chapter 15. Growth in the language and libraries is a witness to the increasing use of Java in new problem domains and applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.48.204