Monday, 21st June 2010
The new frontier for learning Java

Writing your first program

From WikiJava

Jump to: navigation, search
The author suggests:

buy this book


To write your first application in Java is very easy, you just need a text editor (notepad would be fine, but my preference goes to notepad++ which looks better.

Contents

the article

The file

A java program is basically one or more textual files containing the source code for the program.

In this first example I'll guide you through writing a simple single file program.

This is the traditional first program every developer does when starting: Hello World.

The first thing to do is to create teh textual file that will contain your program.

Java defines specific rules for the name of this file:

  • The extension must be '.java'
  • The filename must be the same as the public class contained in.

What does the second point mean? What is a class?

Class is a fundamental concept in Object Oriented Programming (OOP). For the sake of simplicity in this example it's sufficient to say that a class contains a data structure and some methods that can manipulate the data structure.

The way to define a class is using the keyword

class

for example

public class HelloWorld

defines a public class called 'HelloWord'.

Inside the class

In a Java file there must contain only one public class so it's automatic that the name of the file (seen the rules above) must be 'HelloWord.java'.

Inside a class you can insert a data structure, in form of variables that contains values. And you can insert methods which are groups of commands that can be executed by the JVM and operate according to the given variables.

Java defines a rule that a class to be executable directly from the JVM must contain a method called main that must be defined as follows:

public static void main(String[] args)

It must be 'public', 'static', it must return void and must get as parameter an array of String called 'args'.

In sum these special keywords means that the main method must be executable even if the class is not instanciated, that it must be executable by any other method within the JVM and the Class Loader and that whoever calls the main method won't expect any return value at the end of the execution.

The main method is the entry point of any Java program. In other words to be executed a java program must contain at least one main method inside a class and everytime the program is executed you must choose one main method to execute, because only one can be the entry point.

Comments

One last detail before going to the code, we can write comments in almost any point of our program. The comments can be used to explain what the program is doing or to exclude the execution of some parts of the code.

Since the code is not always easy to understand from humans comments are very useful to help them grasp what the code does. Or in some other cases comments are useful to understand what's wrong with a program (home made debug), when it's not doing what it's supposed to.

There are two forms of comments:

The first starts with the string

/*

(forward slash followed by a star) and ends with the string

*/

(star followed by a forward slash). Java Ignores everything included within these two boundaries and we can write inside them whatever we want.

the second form of comment starts with the string

//

(double forward slash) and ends at the end of the same line.

Inside the method

Inside the main method we can finally insert the commands. The commands can be of several types:

  • They can be declarations, for example:
Integer a;

Where you are creating a variable called 'a' and saying that this variable is of type Integer. You are not giving any value to 'a' so it's value it's initially a special value called 'null'.

  • They can be assignments, for example:
a = 5;

where you are giving to 'a' the value '5'.

if (a == 5) System.out.println("Hello World!!!");

Which calls the method 'System.out.println(String);' only if 'a' has value '5'.

All the commands can contain calls to methods in several points of their body.

Normally the commands are executed in the same order in which they are written and the only way to modify this order is to use one of the constructs for controlling the flow.


In this simple example our main method has only one command which is:

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

This command calls the method 'System.out.println(String);' with parameter "Hello World!!!" which is a String.

This command is looks like it's not listed in the previous list, but is in fact an assignment. The fact that 'System.out.println(String);' is defined in the APIs not to return any value makes useless to write a complete assignment in the form 'variableName = value'.


HelloWorld.java

/**
 * Hello World
 *  
 * @author Giulio
 */
public class HelloWorld {
 
    /**
     * 
     * Prints on the default output a greeting to the world.
     *  
     * @param args   
     */
    public static void main(String[] args) {
	System.out.println("Hello World!!!");
    }
 
}

What to do

To create our first Java program we can just copy the above source code inside the 'HelloWord.java' file and save it in a directory, for example 'c:\javaPrograms'.

The program is then ready to be compiled and executed


Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Title (required):

Website:

Comment: