Wednesday, March 23, 2011

The Java programming language API

With the easier use of computers, will be increasingly difficult and complex task that must be done by the programmer. We simply write console-based programs using several subroutines to display the message and take input from the user. GUI (graphical user interface = graphical user interface), such as windows, buttons, scroll bars, menus, text input boxes, and others may be more attractive to users. However, programmers have to deal with the possibility that very very much, and more subroutines that must be made to be able to control every component of the GUI.

Someone who wants to create a program for Windows for example, must use the Toolbox Windows, which is a collection of thousands of subroutines, for example to open and close the window, to draw and write text, add a button and receive a response from the user. In addition there are also GUI subroutines that deal with open, read, and write files, communicate with the network, and others. The point in the toolbox are all things that we think is standard. Apple and Linux have their own toolbox that is different from that provided by Windows.

Each project of the program will consist of innovation (something very new) and reuse something that has ever made before. A programmer is given the tools to succeed in these projects, the most fundamental is what is provided by the programming language itself, such as variables, looping, and others. On it, the programmer can add a toolbox that contains a subroutine that has been written to perform specific tasks. This tool, if designed well, can be a real black box, namely a black box that we really do not need to know how he works.

Innovative part is how to assemble the tool into something useful for a project or a problem to be solved (word processors, browsers, games, etc.). This is called application programming.

Toolbox software is also a black box. It has a user interface with programmers. This interface contains the specification of what routines are there, what parameters are required, and what tasks they do. This information is called the API (Applications Programming Interface = Application Programming Interface). Windows API is the specification of all subroutines that are available in Apple's toolbox.

Companies that make the hardware, such as sound cards or blue tooth may also be to make the API for the device that contains the necessary subroutines for programmers to be able to use that device. Researchers are creating programs to solve a complex mathematical calculations - such as differential equations - also will make the API so that others can use subrutinnya without having to understand in detail how the calculation was done.

The Java programming language API is also supported by a fairly large and standard. Examples of the API that we've seen for example Math.random (), String data types, and subroutines related. Java API consists of standard subroutines to create and control the GUI, to communication networks, read and write files, and others. It may be easier if the API is considered as part of the Java language, but in fact the API consists of subroutines that can be used in Java programs.

Java is a language that does not depend on the machine or operating system on which it runs. Java APIs that together should be able to work with all systems. But remember that the same is the inter-face. Implementation can be very different from one operating system to another.

Java on a computer system consists of the implementation on the computer. Java programs to call subroutines that we make on the computer. When a computer running the program and came to the command to call a subroutine on the API, the computer will run a subroutine in accordance with the implementation of a computer system in which the program is run. That is, we just need to learn one API just to do the programming on a variety of computer and operating system.

Like subroutines in Java, the subroutine in the standard API are grouped into classes. To make the organization better, Java classes can be merged into the package. We can also create a hierarchy of higher package, because the package can also contain other packages. In fact all the standard Java API is implemented in several packages. One package named "java", which contain packages of non-GUI and AWT-based GUI. Another package, ie "javax", which added in version 1.2, contains a class that is used for GUI Swing.

A package can consist of classroom or other use. The package contained in another package called "sub-package." Java and javax package also has several sub packages. One of the sub-package java, for example, called awt. Because inside awt java package, the full name is java.awt. This package actually has a GUI AWT classes, such as the Button class that represents the button, or the Graphics which is a collection of routines for drawing on the monitor. Because this class is incorporated in the java.awt package, then the full name is java.awt.Button and java.awt.Graphics. Likewise, a sub javax package named javax.swing, which consists of a named class javax.swing.JButton and javax.swing.JApplet.

Java package itself consists of several sub-packages, such as java.io we have used in previous chapters, useful to provide facilities input / output, java.net to control network communications, and java.applet implement basic functions for the applet. The most basic package called java.lang. Inside this package there are fundamental classes such as String and Math.

It may be more obvious if we see the following illustration of the level of java packages, sub-packages, classes in the sub packages, and subroutines in the classroom.

Suppose we use the class java.awt.Color in a program. One way is to use the full name of that class, eg

warnaKotak java.awt.Color;


to mendekalarasi variable named warnaKotak which type is java.awt.Color. Of course it will be too tired if we have to write the complete name of the class each time it is used. Java allows us to use directly the name of the class if we give the command import, such as

import java.awt.Color;

at the beginning of the program, and then in other parts, we can abbreviate java.awt.Color only by the name of the class only, so be

Color warnaKotak;


(The only advantage given import command is to abbreviate the writing of the name of the class, not to import the entire class into our program. Should not we give import command, and we call by his full name, Java will still be able to access the class.)

There is another short way to import all classes in a package. We can import all the classes in the java.awt package with

import java.awt .*;

Remember earlier in our code which is shaped like this?

import java.io. *;


When the program that we will create even greater, then there is the possibility we have to import a lot of packages. One important thing to remember is that there is a possibility some of the packages have the same class name. For example, java.awt and java.util have a class named List. If we import the two classes with a command java.awt .* and java.util .*, then the name list became confused.

If we try to create a variable with data type List, java will display an error message about the class name confusing. The solution? Use the full name of the class, namely java.awt.List or java.util.List.

Because the package java.lang .* is a package that is very important, every class in the java.lang will be automatically imported into all programs. As if all programs started with the command "import java.lang .*;". That is, we can call such as the String class, not java.lang.String, or Math.sqrt () instead java.lang.Math.sqrt ().

Programmers can also create a new package. For example we will create a class that will be placed in a package named alatbantu. So at the beginning of our program must be defined with the command "package alatbantu;" Other programs that use this package can add "import alatbantu .*;" for all classes in the package mengabmbil alatbantu.

Problem is that this is a bit more complicated. Remember that when a program using the class, the class must be "available" when the program is compiled and executed when the program continues. Surely more appropriate depending on the Java environment that we do. package is usually called alatbantu must be in alatbantu directory, and directories should be placed in the same directory with the programs that will use the class.

In programs that use a lot of class, it makes perfect sense if we divide and organize the class in one or several packages. And also makes sense to create a new package as a toolbox or in other words create a new API that has not been covered by the Java API. (Often the people who make the API more flattered to his greatness than those who only use the API).

In the Eclipse program, we have used the package from scratch. This chapter describes more details about the package. Actually there is nothing wrong we organize every program we've created since the beginning. At the time of our program is large, then changes are needed to organize would be more significant.

No comments:

Post a Comment

 
THANK YOU FOR VISITING