Java is now one of the most widely used languages across most, if not all, IT sectors. A major part of its attraction has to be that it is free. That's right, most implementations cost nothing! Yet it's supported by a major American corporation: Sun Systems.
There's a huge amount of documentation available on line, not least the high quality offerings from Sun themselves
Java programs are stored in a file with any valid name and the extension .java. It is normal to place this file within a directory having the same name as the .java file. Thus, in Unix, the path to helloworld.java would be ../helloworld/helloworld.java.
The following is a suggested skeleton for java source code…
/* -----------------------------------------------------------------------------
Author: your name
File: filename.java
Purpose: brief description of what the program attempts
History: CCYYMMDD | NAME | Actions
----------------------------------------------------------------------------- */
import group.library.object
import group.library.object
import group.library.object
public class filename extends BaseClass implements ActionMethod
{
variable definition;
constant definition;
// ---------------------------------------------------------------------------
// Constructor
// -----------
// Called automatically as Java runs the program.
// ---------------------------------------------------------------------------
public filename()
{
variable definition;
constant definition;
code;
}
// ---------------------------------------------------------------------------
// Method Definition
// -----------------
// Called from elsewhere in the program.
// ---------------------------------------------------------------------------
public methodname(arguments)
{
variable definition;
constant definition;
code;
}
// ---------------------------------------------------------------------------
// Method Definition
// -----------------
// Entry point for the program.
// ---------------------------------------------------------------------------
public static void main(String args[])
{
code;
code;
}
} // end of class filename
// More classes (which must not include a main() method)...
This is not that different from any other modern compiled language and, in fact, Java deliberately borrows some of the principal syntactic structure of C and C++, such as using braces to delineate code blocks and permitting free-form layout, by requiring logical lines to be terminated with a semi-colon.
As with many object oriented languages, Java provides the 'new' operator for creating instances of objects. There's an interesting view of its operation in this Dr Dobbs article.
Every Java program (also known as an application) must include a class, and only one class, which defines a method called main(). This constitutes the entry point for the program.
The main() method must be public, so that it can be seen from other classes and is defined as static, which means that the method belongs to the class and not to any instance (object) of that class. The main() method returns nothing, so it is defined as void. The main() method should always accept a String array as its sole parameter and this is usually called args as it is intended to pick up any command line arguments passed from the Java run-time call, such as:
java salesreporting -region North
…which will pass the arguments ”-region” and “North” to the application “salesreporting”.