What are the characteristics of Java

1.2 Properties of Java

1.2.1 Language features

Java has been completely redesigned. The designers tried to mimic the syntax of the C and C ++ languages ​​as much as possible, but dispensed with much of the complex and error-prone features of both languages. They have summarized the result of their efforts as follows:

"Java should be a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high-performance, concurrent, dynamic programming language."

The success of Java is closely related to the fact that a substantial number of these requirements have actually been met in a manner that is acceptable to many programmers - although we will also make some critical comments at the end of this chapter.

Java is both an object-oriented programming language in the tradition of Smalltalk and a classic imperative programming language modeled on C. In the details, Java differs quite significantly from C ++, which makes the same claim. By integrating a large number of sophisticated features such as multithreading, structured exception handling or built-in graphic capabilities, Java implements a number of interesting innovations in the field of programming languages.

Java also benefits from the fact that many of the features of C ++ Not and the language became lean and clear. For example, there are no explicit pointers, no separate header files, no multiple inheritance and no templates in Java. However, anyone who thinks Java is a programming language that only offers the bare essentials is wrong. In fact, Java is an elegant language that has all the necessary reserves even for larger projects and demanding tasks.

In Java there are most of the elementary data types that C also has. Arrays and strings are implemented as objects and anchored both in the compiler and in the runtime system. Methodless structure types such as or do not exist in Java. All primitive data types are signed and their size is specified exactly. Java has a built-in logical data type.

Java offers semi-dynamic arrays, the initial size of which can be set at runtime. Arrays are viewed as objects that have some well-defined properties. As in C, multi-dimensional arrays are realized by nesting simple arrays one inside the other. You can also create non-rectangular arrays. All array accesses are checked for compliance with the range limits during runtime.

The expressions in Java are largely the same as in C and C ++. Java has a statement, a, and a loop, and a statement. There are the - and - statements known from C in normal and labeled form. The latter makes it possible to skip more than one loop boundary. Java does not have a statement (although it is a reserved word). As in C ++, variable declarations are viewed as instructions and can appear anywhere within the code. Since version 1.4 Java has an instruction that can be switched on and off at runtime.

As an OOP language, Java has all the properties of modern object-oriented languages. Like C ++, Java allows the definition of classes from which objects can be created. Objects are always treated as reference data types that can be created and used like variables. There are constructors for initialization, and an optional FinalizerMethod that is called when the object is destroyed. Since version 1.1 there are local classes that are defined within another class.

All method calls in Java are dynamic. Methods can be overloaded, but operators cannot. Unlike in C ++, late binding is activated by default, but can be deactivated using a method. Java allows single, but not multiple inheritance of implementations. With the help of Interfaces (these are abstract class definitions that only consist of methods) a restrictive form of multiple inheritance is possible, which represents a compromise between the two alternatives. Java allows the definition of abstract base classes that contain both concrete and abstract methods.

In addition to instance variables and methods, class variables and methods can also be defined. All elements of a class definition can be restricted in their visibility with the help of the keywords known from C ++. There aren't any, but the visibility of methods or classes can be restricted to your own package. Object variables are implemented as references. With their help, pointer processing is restricted compared to C / C ++, which enables the creation of dynamic data structures.

Memory management in Java is automatic. While the creation of objects (with a few exceptions) always requires an explicit call to the operator, memory that is no longer required is returned automatically. A garbage collector that runs as a low-priority background process searches for objects that are no longer referenced at regular intervals and returns the memory they have occupied to the runtime system. Many of the errors that occur when programming in C or C ++ because the developer is responsible for memory management can no longer occur in Java.

There is structured exception handling in Java. This makes it possible to recognize runtime errors and to handle them in a structured way. A method must either catch any runtime error that may occur during its processing or pass it on to the caller by means of a suitable declaration. This in turn has the duty to take care of the error. Exceptions are normal objects, and the associated classes can be extended and used as the basis for application-specific error objects.

1.2.2 Applets: A new class of programs

One of the most widely used explanations for the surprising success of Java is the language's close connection to the Internet and the World Wide Web. With the help of Java it is possible to develop programs that can be distributed over the web and executed within a browser such as Netscape Navigator, SUN HotJava or Microsoft Internet Explorer. For this purpose, the HTML language has been expanded to include the APPLET tag. It thus offers the possibility of embedding compiled Java code in normal web pages.

A Java-enabled browser contains a Java interpreter (the Java virtual machine, also for short VM called) and the runtime library that is needed to support the execution of the program. The exact description of the virtual machine is part of the Java specification, and Java VMs have been ported to practically all major operating system platforms. An applet can thus be viewed as a new type of binary program that is portable across various hardware and operating system platforms and can be easily distributed on the Internet.

In contrast to the limited possibilities offered by script languages ​​such as JavaScript, applets are complete Java programs that can use all the features of the language. In particular, an applet has all the properties of a graphic output window and can be used to display text, graphics and dialog elements. One of the great advantages of applets over conventional programs is that they are easy to distribute. Instead of explicitly executing installation routines, the Classloader the browser simply takes the components of an applet from the network and executes them directly. This is particularly useful for small and medium-sized applications in a local network environment, especially if they change frequently.

Security was one of the most important design goals in Java development, and there are a number of security mechanisms that are designed to prevent Java applets from causing damage while they are running. For example, an applet running in a web browser is not allowed to perform file operations on the local computer or to start external programs.

It should not be concealed, however, that the applet euphoria has clearly subsided over time. On the one hand, this was due to the Java implementations of the browser, which could not keep up with the JDKs. In addition, the security features often placed so great restrictions on the developers that they restricted the real usefulness of the applets. The great success of Java in the post-1.1 era can be seen more in the development of applications than applets. With the publication of the WebStart-Technology during JDK 1.3, the advantages of both technologies were combined again.

1.2.3 Graphics programming

The Java runtime library offers extensive graphical capabilities. These are essentially platform-independent and can be used to equip portable programs with GUI capabilities. Since version 1.2 of the JDK, these capabilities have been referred to as Java Foundation Classes (short JFC), the three most important components of which are:

  • The Abstract Windowing Toolkit (short AWT) offers elementary graphics and window functions based on the capabilities available on the respective target machine.
  • The Swing toolset In addition, it provides a number of additional dialog elements and enables the construction of very complex graphical interfaces. With his Pluggable look and feel it offers the possibility of switching the look and feel of a program at runtime and adapting it to the needs of the respective user and the capabilities of the system environment.
  • The third important component is that Java 2D API, which provides complex graphic operations and image processing routines.

It is a remarkable innovation that elements for GUI programming are in a programming language portable to provide. In principle, there were programming languages ​​that had graphical capabilities in the past, but anyone who had the task of providing a graphical user interface under Windows, OS / 2, UNIX and on the MAC usually had considerable porting effort. In any case, this was not possible with the standard tools of the C or C ++ languages ​​and their runtime libraries. With Java and its class libraries, an easy-to-use language is now available for the first time, which already offers the creation of GUI programs as a core functionality.

The AWT provides a number of elementary operations to generate graphic output elements such as lines, polygons, circles, ellipses, circle segments or rectangles. These methods can also be used in a fill mode, which ensures that the drawn areas are filled with color. As in most graphics libraries, Java also uses the concept of a Graphic context, which forms an abstraction of the actual output device.

In addition to graphic elements, text can of course also be output and placed anywhere within the window. Text can be scaled and it is possible to work with different fonts. The AWT endeavors to offer a portable way of font selection by offering a number of elementary fonts across platform boundaries. With the help of font metrics, numerical properties of the fonts used can be determined and taken into account in the output.

The color model of Java is based on the RGB model, which determines its colors additively on the basis of the contained red, green and blue components. The HSB model is also supported (hue, saturation, brightness), and there are methods to convert between the two. The color system supports a number of predefined colors that are available across platforms.

In addition to graphics, sound can also be output. Java supports the rendering of au files (a format introduced by SUN for storing digital sound samples) and since version 1.2 also wav- and aiff filesthat can be downloaded either from the Internet or from a local file. The samples can be played once or repeated in a loop. It is also possible to play two or more sound files at the same time. Since JDK 1.2 there has been its own sound API, which in addition to wave files also Midi files can play back and edit.

The AWT allows the display and manipulation of image data. Using standard methods, graphics in elementary formats such as GIF or JPEG can be loaded, scaled and displayed on the screen. In addition, there is the package that was designed for manipulating image data and provides sophisticated functions for image and color manipulation.

As in most graphic development environments, the program flow is controlled by messages in the AWT. When certain events occur, they are sent to the program and handled by it in an appropriate manner. Java provides messages for processing mouse, keyboard, window, dialog and many other events. Event handling since JDK 1.1 allows messages to be sent to any object that implements the interface of a message receiver.

The second graphical user interface of the JDK, the Swing Toolkit, offers even more extensive capabilities than the AWT. A completely new architecture was designed for this, and many additional dialog elements such as tables, trees or index cards are available. Instead of relying on the properties of prefabricated GUI elements as in the AWT, Swing only uses a very limited set of platform-specific graphic operations. All dialog elements are represented using simple and portable graphic primitives. The number of differences between the various platforms is drastically reduced in this way, and the Swing dialog elements can be ported much more easily and consistently to different graphics systems.

1.2.4 Extensive class library

With a whole range of useful classes and interfaces, the Java class library offers the possibility of programming in a very problem-oriented manner. Some of these features are useful right from the start, others only become available after a certain amount of training.

In addition to graphic output options, Java also provides simple text output, similar to the corresponding functions in C or C ++. This makes it possible to equip programs with simple, line-oriented input / output options if no complex user interface is required. Simple text outputs are generated with the methods of the class. These allow all common data types to be output in a terminal window. The class variable offers a predefined one that is initialized by the runtime system. In a similar way, there is also the option of reading in simple text entries from the keyboard.

One of the most important elements of the class library is the class, the Java implementation of strings. offers a variety of important methods for manipulating and accessing character strings, such as operations for numeric conversions, character and substring extraction, as well as for text search and string comparison.

Interestingly, a String object can no longer change after its initialization, but always retains its original value. What initially looks like a severe restriction is usually meaningless in practice. Because in cooperation with the class (which represents strings of variable length) and the ability of the compiler to use them automatically for string initialization, assignment and concatenation, this fact usually remains hidden from the programmer. Thanks to the automatic memory management and the efficient conversion from to, handling strings from the programmer's point of view is similar to handling strings of variable length in other programming languages. Because of the automatic memory management, Java strings are much more secure than zero-terminated strings in C or C ++.

A in Java is a linear list that can hold any type of object and its elements can be accessed both sequentially and randomly. The length of a vector is variable, and elements can be inserted at the end or at any other point. Because of this flexibility, a can often be used where otherwise a linear list would have to be created manually by concatenating object references. As usual, the memory management of a vector is completely automatic. There are also other container classes. For example, it offers the option of storing key-value pairs together and efficiently retrieving the associated value for a given key.

A useful mechanism for traversing container classes is the interface, which makes the methods and available. These can be used to step through all elements of the container in a loop.All predefined container classes provide methods that return enumeration objects for traversing their own elements.

Since JDK 1.2 there is a separate library for container classes in Java, the Collection API. It provides a comprehensive collection of interfaces for container classes and offers different implementations for different use cases. The previously mentioned class is replaced here by the interface, which is easier to use. The Collection API also provides some algorithms for processing containers (e.g. sorting) that did not exist in the older container classes.

Java also provides random numbers. The package offers a class that enables the initialization of random number generators and access to integer or floating point random numbers. Next evenly distributed represents the class too normally distributed Random numbers available.

Since JDK 1.1, further highly specialized (and sometimes very complex) libraries have been made available with each release. For example, JDBC (Java Database Connectivity) access to relational databases, JavaBeans provides a portable component architecture, and with the Networking API, RMI (Remote method invocation) and Java’s own CORBA implementationjavaidl network resources and distributed objects can be accessed company-wide. By Serialization objects can be made persistent, and with the Reflection API the structure of objects and classes can be examined at runtime and accessed dynamically. We will explain in detail the most important of these libraries in this book.