Monday, 3 May 2010

Language Fundamentals : Main methods and Java program structure

The Structure of Java Programs
Java programs are composed of declarations of classes and interfaces. Classes define variables, which
provide named access to data, methods, which perform actions consisting of operations on the data,
and constructors, which create instances of classes, referred to as objects. Data items consist of
primitive data values—such as byte, char, and int values—and objects—such as arrays, I/O
streams, and GUI elements.

Interfaces define collections of methods that are implemented by classes. They are also used to define
constants, which are data values that cannot be changed.
Java programs are written using one or more compilation units, which are Java source code files. Every
source code file consists of the name of a class or interface followed by the .java extension. Since java identifiers are case-sensitive, source code filenames are also case-sensitive.

Source file : Each source code file may contain at most one public class or interface. If a class or interface is
declared as public, the source code filename must be the name of the class or interface (followed by
the .java extension). If a source code file does not contain a public class or interface, it may take on
a name that is different from its classes and interfaces.

Identifying Packages
Java classes and interfaces are organized into packages. Packages provide a naming context for
classes and interfaces. In other words, packages enable different programmers (or even the same
programmer) to create classes and interfaces with the same name. For example, if you and I both
create a class named Cool and then use the two different versions of Cool in the same program, the
compiler and runtime system won't know which version to use. But, if I put my Cool class in the My
package, and you put your Cool class in the You package, the compiler and runtime system will have
no problem, as long as we refer to Cool using its package name.
Packages are identified by the package statement. It must appear as the first statement in a source
code file
package packageName;
Note Use of Packages In addition to being used as a naming context, packages are used to organize related classes and interfaces into a single API unit to which access may be controlled.
If a package statement is omitted, the classes and interfaces declared within the package are put into
the default no name package. In the Java 2 Platform Software Development Kit (SDK), the package
name and the CLASSPATH environment variable are used to find a class or interface that is located in
another package.
Importing Classes and Interfaces from Other Packages
The import statement is used to reference classes and interfaces that are declared in other packages
(without having to specify their names each time they are referenced). There are three forms of the
import statement:
import packageName.className;
import packageName.interfaceName;
import packageName.*;
The first and second forms enable the identified classes and interfaces to be referenced without
specifying the name of their package. The third form allows all classes and interfaces in the specified
package to be referenced without specifying the name of their package.

The Main method and command line arguments

No comments:

Post a Comment