Friday, July 16, 2010

Getting Started With Java

Getting Started -- Before you begin
by Peter Smaith

1. You will need a text editor to write your code. Simple editors such as gedit (on Linux) or notepad (on MS Windows) will be adequate to get you started.

2. You will need the Java development kit (JDK) to compile your code.

3. You will need the Java run-time environment installed to run your programs.

4. Alternatively you can use an integrated development environment (IDE) such as NetBeans or Eclipse. In the case of NetBeans the necessary compiler and run-time environment will be setup on your system.

Your First Java Program


This is a very simple Java program


class First


public static void main(String[] args)


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



To use this source code cut and paste it into your text editor and save the file as "". To compile the program use the javac compiler. This translates the source code into byte code suitable for execution by the java run time environment. You need only compile the program once (after each modification). To run the program use the java command. So, to compile and run from the command line:


$java First

Hello World



1. The Java compiler, javac, expects the file name with the extension java.

2. The file name must match the class name within the code (in this case First).

3. Java is case sensitive, First is different to first.

4. The java command does not expect a file extension.

Program elements


Comments are placed in the source code for the benefit of the developers and maintainers of the code. They are not executed at run time. Java supports three types of comment.

1. /* Comment style 1. These can be multiline. This type of comment is shown in the example above. */

2. /** Comment style 2 These can be multiline and are used as part of the code documentation system.*/

3. //Comment style 3. These are singe line.

The class statement

Java programs are made up of units called classes. The classes contain variables and methods to manipulate those or other variables. The example above has one class called "First". First contains one method called "main".

The main method

Every Java application has a method called main (although it is hidden in system code in many cases). The main method is the entry point of the program, that is, it is the method that is executed when the program is started. The main method is always declared public void static. This has the following effects:

* The public keyword means the method can executed from outside the class containing it.

* The void keyword means the method returns no data.

* Roughly speaking, the static keyword makes the accompanying method or variable an intrinsic property of the class. For example, an intrinsic property of a square is that it has four sides. Every square by definition has four sides. The numerical length of the sides of square is not intrinsic, different instances of a square have different sizes.

A text string is a sequence of characters, for example the text you are reading is a string. A String in Java is an object for storing and manipulating text strings. The String object stores the text as unicode characters. An array is a list of similar data items (in this case Strings) that can be accessed and manipulated by a integer index. Arrays in Java are indicated by the square brackets, "[]". A method gets information from the method that called it via its parameters. In the case of the main method the only parameter is "String[] args" this is an array of Strings called "args". In the above example we ignore parameter passed to main.

The action of our main method In the program the main method simply prints the literal text string "Hello World". This is achieved by executing the "println" method in the object "out". The object "out" is a field of the class "System". Note the syntax used in the program for expressing this arrangement.


* A Java program is complied using the javac command and once compiled it can run using the java command.
* A Java program is composed of classes.
* A Java program has a method called main which is executed when the program starts.
* The main method is declared as "public static void" and takes one, argument an array of strings.

Peter Smaith has over 2o years experience in the computer industry, having worked as software engineer, manager and consultant. Peter is an occasional reviewer at

Article Source: