Friday, 4 May 2018

Main Features of the Java Programming Language

The Java TM programming language follows C++ to some degree, which carries the benefit of it being familiar to many programmers. This section describes the essential features of the Java programming language and points out where the language diverges from its ancestors C and C++.

1. Primitive Data Types

Other than the primitive data types discussed here, everything in the Java programming language is an object. Even the primitive data types can be encapsulated inside library-supplied objects if required. The Java programming language follows C and C++ fairly closely in its set of basic data types, with a couple of minor exceptions. There are only three groups of primitive data types, namely, numeric types, character types, and Boolean types.

Java Programming Language, Oracle Java Guides, Oracle Java Learning, Oracle Java Tutorials and Materials

Numeric Data Types

Integer numeric types are 8-bit byte, 16-bit short, 32-bit int, and 64-bit long. The 8-bit byte data type in Java has replaced the old C and C++ char data type. Java places a different interpretation on the char data type, as discussed below.

There is no unsigned type specifier for integer data types in Java.

Real numeric types are 32-bit float and 64-bit double. Real numeric types and their arithmetic operations are as defined by the IEEE 754 specification. A floating point literal value, like 23.79, is considered double by default; you must explicitly cast it to float if you wish to assign it to a float variable.

Character Data Types

Java language character data is a departure from traditional C. Java's char data type defines a sixteen-bit Unicode character. Unicode characters are unsigned 16-bit values that define character codes in the range 0 through 65,535. If you write a declaration such as
char  myChar = `Q';

you get a Unicode (16-bit unsigned value) type initialized to the Unicode value of the character Q. By adopting the Unicode character set standard for its character data type, Java language applications are amenable to internationalization and localization, greatly expanding the market for world-wide applications.

Boolean Data Types

Java added a Boolean data type as a primitive type, tacitly ratifying existing C and C++ programming practice, where developers define keywords for TRUE and FALSE or YES and NO or similar constructs. A Java boolean variable assumes the value true or false. A Java programming language boolean is a distinct data type; unlike common C practice, a Java programming language boolean type can't be converted to any numeric type.

2. Arithmetic and Relational Operators

All the familiar C and C++ operators apply. The Java programming language has no unsigned data types, so the >>> operator has been added to the language to indicate an unsigned (logical) right shift. Java also uses the + operator for string concatenation; concatenation is covered below in the discussion on strings.

3. Arrays

In contrast to C and C++, the Java programming language arrays are first-class language objects. An array in the Java programming language is a real object with a run-time representation. You can declare and allocate arrays of any type, and you can allocate arrays of arrays to obtain multi-dimensional arrays.

You declare an array of, say, Points (a class you've declared elsewhere) with a declaration like this:

Point  myPoints[];

This code states that myPoints is an uninitialized array of Points. At this time, the only storage allocated for myPoints is a reference handle. At some future time you must allocate the amount of storage you need, as in:
myPoints = new Point[10];

to allocate an array of ten references to Points that are initialized to the null reference. Notice that this allocation of an array doesn't actually allocate any objects of the Point class for you; you will have to also allocate the Point objects, something like this:
 int  i;
    for (i = 0;  i < 10;  i++) {
[i] = new Point();

Access to elements of myPoints can be performed via normal C-style indexing, but all array accesses are checked to ensure that their indices are within the range of the array. An exception is generated if the index is outside the bounds of the array.

The length of an array is stored in the length instance variable of the specific array: myPoints.length contains the number of elements in myPoints. For instance, the code fragment:

howMany = myPoints
would assign the value 10 to the howMany variable.

The C notion of a pointer to an array of memory elements is gone, and with it, the arbitrary pointer arithmetic that leads to unreliable code in C. No longer can you walk off the end of an array, possibly trashing memory and leading to the famous "delayed-crash" syndrome, where a memory-access violation today manifests itself hours or days later. Programmers can be confident that array checking in Java will lead to more robust and reliable code.

4. Strings

Strings are Java programming language objects, not pseudo-arrays of characters as in C. There are actually two kinds of string objects: the String class is for read-only (immutable) objects. The StringBuffer class is for string objects you wish to modify (mutable string objects).
Although strings are Java programming language objects, Java compiler follows the C tradition of providing a syntactic convenience that C programmers have enjoyed with C-style strings, namely, the Java compiler understands that a string of characters enclosed in double quote signs is to be instantiated as a String object. Thus, the declaration:

String hello = "Hello world!";

instantiates an object of the String class behind the scenes and initializes it with a character string containing the Unicode character representation of "Hello world!".
Java technology has extended the meaning of the + operator to indicate string concatenation. Thus you can write statements like:

System.out.println("There are " + num + " characters in the file.");

This code fragment concatenates the string "There are " with the result of converting the numeric value num to a string, and concatenates that with the string " characters in the file.". Then it prints the result of those concatenations on the standard output.
String objects provide a length() accessor method to obtain the number of characters in the string.

5. Multi-Level Break

The Java programming language has no goto statement. To break or continue multiple-nested loop or switch constructs, you can place labels on loop and switch constructs, and then break out of or continue to the block named by the label. Here's a small fragment of code from the Java programming language built-in String class:

test:  for (int i = fromIndex; i + max1 <= max2; i++) {

           if (charAt(i) == c0) {
               for (int k = 1; k<max1; k++) {

                   if (charAt(i+k) != str.charAt(k)) {

                       continue test;


               }     /*  end of inner for loop  */


       }             /*  end of outer for loop  */
The continue test statement is inside a for loop nested inside another for loop. By referencing the label test, the continue statement passes control to the outer for statement. In traditional C, continue statements can only continue the immediately enclosing block; to continue or exit outer blocks, programmers have traditionally either used auxiliary Boolean variables whose only purpose is to determine if the outer block is to be continued or exited; alternatively, programmers have (mis)used the goto statement to exit out of nested blocks. Use of labelled blocks in the Java programming language leads to considerable simplification in programming effort and a major reduction in maintenance.

The notion of labelled blocks dates back to the mid-1970s, but it hasn't caught on to any large extent in modern programming languages. Perl is another modern programming language that implements the concept of labelled blocks. Perl's next label and last label are equivalent to continue label and break label statements in Java.

6. Memory Management and Garbage Collection

C and C++ programmers are by now accustomed to the problems of explicitly managing memory: allocating memory, freeing memory, and keeping track of what memory can be freed when. Explicit memory management has proved to be a fruitful source of bugs, crashes, memory leaks, and poor performance.

Java technology completely removes the memory management load from the programmer. C-style pointers, pointer arithmetic, malloc, and free do not exist. Automatic garbage collection is an integral part of Java and its run-time system. While Java technology has a new operator to allocate memory for objects, there is no explicit free function. Once you have allocated an object, the run-time system keeps track of the object's status and automatically reclaims memory when objects are no longer in use, freeing memory for future use.

Java technology's memory management model is based on objects and references to objects. Java technology has no pointers. Instead, all references to allocated storage, which in practice means all references to an object, are through symbolic "handles". The Java technology memory manager keeps track of references to objects. When an object has no more references, the object is a candidate for garbage collection.

Java technology's memory allocation model and automatic garbage collection make your programming task easier, eliminate entire classes of bugs, and in general provide better performance than you'd obtain through explicit memory management. Here's a code fragment that illustrates when garbage collection happens:

 class ReverseString {
    public static String reverseIt(String source) {
        int i, len = source.length();
        StringBuffer dest = new StringBuffer(len);
        for (i = (len - 1); i >= 0; i--) {
        return dest.toString();

The variable dest is used as a temporary object reference during execution of the reverseIt method. When dest goes out of scope (the reverseIt method returns), the reference to that object has gone away and it's then a candidate for garbage collection.

7. The Background Garbage Collector

The Java technology garbage collector achieves high performance by taking advantage of the nature of a user's behavior when interacting with software applications such as the HotJava TM browser. The typical user of the typical interactive application has many natural pauses where they're contemplating the scene in front of them or thinking of what to do next. The Java run-time system takes advantage of these idle periods and runs the garbage collector in a low priority thread when no other threads are competing for CPU cycles. The garbage collector gathers and compacts unused memory, increasing the probability that adequate memory resources are available when needed during periods of heavy interactive use.

This use of a thread to run the garbage collector is just one of many examples of the synergy one obtains from Java technology's integrated multithreading capabilities--an otherwise intractable problem is solved in a simple and elegant fashion.

8. Integrated Thread Synchronization

Java technology supports multithreading, both at the language (syntactic) level and via support from its run-time system and thread objects. While other systems have provided facilities for multithreading (usually via "lightweight process" libraries), building multithreading support into the language itself provides the programmer with a much easier and more powerful tool for easily creating thread-safe multithreaded classes.

Related Posts