Tuesday, February 24, 2009

1 - Introduction to Java Programming

Java is Object Oriented Programming Language. You can find many explanation of java environment and architecture somewhere else. This page is focusing on the programming example. If you see the examples in this page you will notice that a java program at least consist of a class declaration, method declaration, comments (if required/optional), and variable declaration (usually a class has several variables).

Our first program is hello java. The source code is presented below. All you have to do is copying and pasting it into your text editor, compile, and run the program :

public class HelloJava // The class declaration
{

public static void main(String args[]) //The main method declaration
{
System.out.println("Hello Java : a cup of arabica and robusta blend");
}

}


1. Copy the source code.
2. Paste it into your text editor.
3. Save the program as HelloJava.java IN YOUR WORKING DIRECTORY.
4. Compile the program using this command :javac HelloJava.java
5. After completing compilation process, a bytecode is produced : HelloJava.class
6. You run your program by executing this command : java HelloJava

We have more programming work to do. We are going to have another program that will print multiple lines of output. The source code is presented below. In case you are wondering what kopi luwak is Kopi luwak is coffe beans fermented inside the digestion system of a weasel like animal called luwak.

public class HelloKopiLuwak
{

public static void main(String args[])
{
System.out.println("Kopi Luwak : Exquisite Coffe produced by digestive system of ");
System.out.println("Paradoxurus hermaphroditusa, raccon-like animal." );
System.out.println("Perhaps it costs 300 or 400 dollars per kilo ");
}

}

1. Copy the source code.
2. Paste it into your text editor.
3. Save the program as HelloKopiLuwak.java IN YOUR WORKING DIRECTORY.
4. Compile the program using this command :javac HelloKopiLuwak.java
5. After completing compilation process, a bytecode is produced : HelloJavaKopiLuwak.class
6. You run the program by executing this command : java HelloKopiLuwak

We can also modify the program so that instead of having 3 println statements. We only need one.

public class HelloKopiLuwakModified
{

public static void main(String args[])
{
System.out.println("Kopi Luwak : Exquisite Coffe produced by digestive system of\nParadoxurus hermaphroditusa, raccon-like animal\nPerhaps it costs 300 or 400 dollars per kilo ");
}

}

Then what you have to do is :
1. Copy the source code.
2. Paste it into your text editor.
3. Save the program as HelloKopiLuwakModified.java IN YOUR WORKING DIRECTORY.
4. Compile the program using this command :javac HelloKopiLuwakModified.java
5. After completing compilation process, a bytecode is produced : HelloKopiLuwakModified.class
6. You run the program by executing this command : java HelloKopiLuwakModified

Until now, we only have one class. A one class program is fine inside classroom. But in actual world you need to have several classes. How to make such construct will be discussed on the next section of Java Programming.