The Java Explorer Back | TOC | Next

<Core><Intermediate><Advanced>
Overview | Packages | Class Internals | Collections | I-O | Network | Database 

Even today, six years after its invention, Java remains at its core a simple programming language, thereby fulfilling the promise of its architects. During these many years that Java has been around, a great many features have been added to the core, from web content development to mobile content delivery, from embedded device technology to text formatting and processing capabilities, from enhanced graphics programming to delivering rich multimedia content; Java is the de facto choice for both desktop and wireless software development.

Java development has turned a full circle. Beginning with the aim of providing a simple and extensible platform for programming embedded devices, it veered from this focus and developed as a general purpose programming language, gained sufficient capability to work hand-in-hand with the emerging Web and graduated to a computing platform in its own right, and now once again it has entered the embedded device programming in all earnestness.

At the core is the object-oriented paradigm that has now become ubiquitous, and universally accepted as the designing and programming methodology. Object-oriented programming, or simply OOP, is here to stay, and even the traditional programming languages have either migrated or provided hooks to OOP techniques.

Let us take a look at a simple Java program that stores a person's name and mobile number. 

import java.util.*;       // package reference for Vector class

public class Addresses {      // the program's driver

    public static void main(String[] args) {    
// the entry point of execution

        NameNumPairs np = new NameNumPairs();     
// instantiation

       
// add name/num pairs
        np.addNameNumPairs("nan", "12345678");
        np.addNameNumPairs("van", "45678901");
        np.addNameNumPairs("zan", "23456789");

       
// fetch and display name/num pairs
        Vector pairs = np.getNameNumPairs();
        for(int i=0; i<pairs.size(); i++) {
            System.out.println((String)pairs.elementAt(i));    
// printing on console
        }
   }
}

Save the above code in a file with the name Addresses.java, and the one below in a file named NameNumPairs.java. 

Note: What follows the two forward slashes // is a valid comment, and not processed as part of code. The use of plain and bold style for text is to mark off code per se from its explanation.

import java.util.*;

public class NameNumPairs {

    private Vector pairs = new Vector();    
// the built-in collection class for storing data

    public NameNumPairs() {     
// a class constructor
        // do nothing
    }

   
// accessor method
    public Vector getNameNumPairs() {
        return pairs;
    }

   
// method to store data
    public void addNameNumPairs(String name, String number) {
           pairs.add(name+":"+number);
    }
}

Compile the files with the Java compiler javac. You will find in the bin folder of the <java-install-directory>.Then run with java.exe as under:

java Addresses

The output will be:
nan:12345678
van:45678901
zan:23456789

This is the Java programming style, no matter what you are programming - a web server, a mobile device or a desktop application.

The class Addresses.java is the driver part of the program, while the data is actually stored in an instance of the object of class NameNumPairs. The separation of concerns, namely the program driver and program data, is neat.

The data is actually stored in a collection class called Vector that belongs to the utility package, referenced by the import statement at the beginning of the code. The data is held privately inside the NameNumPairs class, and is accessible only through its public interface - the getNameNumPairs method. If required, the data may also be returned after processing, sorting, filtering or formatting, without exposing the storage mechanism like we have done here.

The program driver class Address.java has a main method that is the starting point of the program. Any class can contain a main method, but only the one that is used on the command line to run is invoked, while all others are ignored.

An instance of the NameNumPairs class is created in the Address class and populated with the data. The keyword new is used for this purpose. There are other ways of providing a new instance of a class, which we shall see as we go along. Every instance is unique and occupies a space in the program's allocated space in the main memory. It is the runtime entity, and lives in the memory as long as the program runs. When the program ends or the method that created it goes out of scope, it is discarded and becomes garbage. The memory it had occupied is reclaimed by the garbage collector, a program that runs in the background and is part of the Java runtime environment.

A class is a static description of a piece of a program's functionality. When it is compiled, it is ready to spawn instances on demand. The string "nan" is actually an instance of the String class, and holds the literal stringified characters "nan". So it may also be written as new String("nan"). String literals are a common programming artifice that all languages provide, meaning that the runtime creates the necessary instances. The terms object and instance in this sense are synonymous. The process of creating an object or an instance is called instantiation.

Data is stored internally in the class NameNumPairs in a Vector, and can be manipulated only by the method that the class provides. The Address class cannot access the data Vector directly since it is held privately in the NameNumPair class. So we have an accessor method that returns the data object to us, and can be accessed only via an instance of the NameNumPairs class. This is a classic example of data encapsulation and hiding - the stuff on which OOP is built.

The OOP vs procedure-oriented debate that had raged in the '90s is over at last. We have now to  understand OOP thoroughly so we can make the best use of it. We will consider OOP concepts in detail as and when we encounter them in our exploration of the Java World.