1.4.1 - Java Packages and Java Classes (Cont.)

Public classes may be directly accessed by the class in the same package, i.e from the files within the same folder. For the access of public classes from other packages, first the package containing the class must be imported. This is realised by using import keyword in the very beginning of the class definition, before all the declarations. The usage of this keyword is as follows:

import [path] packageName;

Specifying path information is not necessary for the packages that are directly visible. But when the folder to be imported is inside of the another folder, direct visibiliy is absent and the path information should be stated. To avoid this it is best to arrange the file structure before beginning any project.

To ensure the visibility of package folders in between, one project folder must be opened and all the package folders must be placed inside the project folder. To avoid the confusion of the compiled class files with textual source files with the same name, source and class files, it is advisable to place source and class files in a separate folders. Source files may be put anywhere in the operating system, but class files must be stored exacly in the folder whose name is the declared package name. In that context, one source folder may be opened in the project folder following by any number of package folders needed, inside the project folder.

Java class files benefit from huge amount of library classes supplied with the Java ecosystem. These supplied libarary class reside in the library folder which is inside the folder where java functionality is stored generally it is in the c:\program files\java folder. These packages also must be imported before using them and their visibiliy from the project folder mudt be ensured. For this, it would be better to place the java folder in the system path. That is why, we have fulfilled this requirement before beginning anything in the project.

Java Virtual Machine (JVM) use mainly two types of the memory areas to accomplish the compilation and running Java programs. The first memory location is called "heap" and there is where the supplied precompiled Java library classes reside. The heap memory is more organised and therefore its access is relatively fast. The second memory location is called the "stack". This memory is more volatile and user defined classes, varaibles etc are stored in the stack. The structure of the stack is less organised and time of access is relatively slow. Java has freed the programmers from bothering the memory organisation of their programs. This is a valuable asset and gives to the prgrammers a sweet sense of relief and comfort. We have given this information only for the sake of completion. We will stricly not bother in what portion of the memory are residing our program elements, we do not have any means also to follow that. We should concentrate on our programming tasks and nothing else, Java has given us this freedom.

The main library packages supplied by the Java system are the following:

java.lang

This is the main package assembling fundemental data types, class definitions, security manager etc... This package is the default package of the Java programs. It is automatically imported to all Java programs without the need of explicit import declaration.

java.io

This package contains language elements used for input and output data.

java.net

This package contains addional classes for network operations.

java.applet

This package is for displaying Java applets in the web browsers.

java.awt

This is for producing fancy windows for Java programs as its name implies (Java Abstract Windowing Kit). Deprecated for the present in favor of swing.

java.swing

New visual aid for programs.

java.util

Additional programming utilities.

There is also less frequented utility packages as well as packages supplied from third parties. One the well known is SWT package (Standard Widgeting Toolkit) supplied by IBM to replace swing.

We have know basic information about Java functionality in the operating systems. We are very near of producing our stand alone Java programs.

1.4.2 - Executable and Inexecutable Java Classes

Thye essential step of gaining Java functionality is the compilation of Java source codes to bytecodes and storing in a bytecode file with an extension *.class. After a class file is generated, its interpretation for producing sensible output is not necessary. The magic word here is sensible and its meaning is some event that you can sense like printing some output. But some classes are not intended to produce sensible results, they are only collections of the useful programming aids and they intended to support other programs.

For any Java program be executable, it should include a special method named main . The main method is public , static and void method. The meaning of a void method is the method will not return any value. It is now time to work on our first executable Java program.

1.4.3 - First Java Program

Our first Java program will conventionally a program that print "Hello World !" to the standard output device which is the command window by default. This will be an executable class including the main method and it will also incude special call to a library program to transfer program results to the standard output device. This is the source code of our first program:

public class HelloWorld() {

public static void main (String[] args) {

System.out.println("Hello World ! ");

}

}

The codes does not declare any package, this is normal for preliminary programs for demonstration purposes. When any package is not stated, the program is understood as being included in the default package. The default package means that the program may be stored, compiled, interpreted (run) from any folder of the operating system.

The program does not explicitly declare any package importations. But, Like all the Java program it will automatically import java.lang package by default and there is no need to explicitly declare this importation. This is exactly the case for this program.

The access modifier of the main() method is public this has the meaning that the main() method may be accessed by other classes. The other classes in the same package may use the main() method of the class HelloWorld(), as HelloWorld.main() but this calling method is for any other method differing from main(). Java has a privilege for the main() mthod, you can call the main() method of any class, direcly as HelloWorld() and the compiler will understand as HelloWorld.main(). For the the classes from other packages, first the package containing the public class must be imported and then we can access to public metods.

The type of the main() method is stated as static. This indicate that the main() method belong to the class itself and not to the instances of this class. For the moment we won't be much concerned about this description, this will be the subject of Object Oriented Programing (OOP).

Finally, the type of the main() method is described as void. This means that the main() method does not return any value. It is executable in a sense that it executes its content but does not return any value. This is the characteristic of any void method. We will present the systematic of the methods short after this chapter and we will be more knowledgeable on the subject. For the moment, we should only know that it will be no more execution steps after the method main(), because it impicitly calls System.exit(0) after the last statement in the main().

The expression given in parenthesis after main (String[] args) is the arguments of the method main(). This defines a String array named args as the arguments of the main() method. This definition comes from the definition of the main program in the Java specification. We will cover Java arrays very soon and all will be further explained when we will cover methods systematically. For the moment, we can take it like an expression that we should declare for the main() method may work.

System.out.println() is a calling expression. System should be a class in the Java.lang package because it is beginning with an uppersize letter. out should be a class member of System because its name is beginning with lowercase letter. Finally, println() should be a method of out. This indicate that the class System has out as the class member, out is of type of a class that we can not know from here and out has println() method. We can not forecast more without controlling java.lang specification. After reading this specification, we understand that the class member out is of type printstream class and this class has other methods to display the results in the console window. The println() method start printing from the beginning of the next line, print() method, prints in the same line. There is even a printf() method exactly like C++, for formatted outputs. But we will generally utilize print() and println() which have most simplest usage. The reason for we will not utilise more sophisticated print()/println() methods of the printsteam() class is that the console output is only utilised for preliminary programs aiming to demonstrate language features. Normaly swing or SWT based windows will be used for the program input/ouput. We will use console outputs only in the beginning chapters of our Java course and only in the demonstration examples.

We can edit the program above with any text editor, store in folder, but name should be HelloWorld.java since the filename should correspond to the class name. HelloWorld.java may be compiled and run as defined in the section 1.1.2,but there is more elegant ways for doing this. Working from the command line in general is not a better way to compile and run Java programs. It may be suitable for simple demonstrative programs, but with larger projects, when source codes must be separated from compiled code, when class files must be stored in their corresponding package folders, one may resort for the more convenient ways to develop a project.

Integrated Development Environments (IDE) 's provide an efficient platform for editing, compiling and running Java programs.There is many reliable IDE, supplied most of the time as freeware can be utilised to develop Java projects. We can state, Scite, Qwined, JGRASP, IntelliJIDEA community Edition IDE, JCreator LE, Geany, Sun's own IDE Netbeans to choose from. But Eclipse platform developed by IBM outperforms all of them and only Netbeans is somrewhat closer. But one can prefer JGrasp for simpicity and ease of utilisation, IntelliJIDEA for somewwhat more eleborated IDE but the quality of the Eclipse platform is undiscutably superior among all others. We will choose Eclipse IDE for developing our example applications. Downloading and installing Eclipse platform will be described in the next section.

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)