Tuesday, February 2, 2016

First Program


Prepare First Program

What is the flow of a Java program?

To make it easy for the noob I am writing the 3 simple steps. Don't worry about the details, when we finish the basic we dive in deep ocean of the Java knowledge.
  • Write the source code in Java programming language
  • compile it, you get a *.class file
  • run the compiled file
  • Remember; Java language is case sensitive. Its mean variable is not equal to Variable.
NB: If you know the term byte code forget about it for the time

How to write a Java program?

Open eclipse IDE and do the following steps:
  1. Got to "File>New>Project...". You will see a dialoug box.

  2.  Choos Java>Java Project from the dialog box.
  3. Click "Next" Button.
  4. Give any name to your project. In my case "HelloWorld" without spacing.
  5. Click "Finish" Button
  6. If ask for the perspective just select yes.
Your project is here.
Notice there is small arrow before the Project Name. It will expend the content of the project.
It has two things
  1. src Forlder
  2. JRE SYSTEM Library

src Folder

It contains all the source code. It is our space we create Java program files and put in this folder. 

JRE SYSTEM Library

It has the core Java libraries. Which are common and necessary for writing the basic Java program. It is provided by the Oracle JDK. We will see in depth these libraries in future.

Create Source File 

We write the Java program in a text file. This file is called source file, although it is a text file but we use its extension *.java
Follow the steps to create a source file in Eclipse IDE:
  1. Right click on HelloWorld project in left pane.
  2. It will show you a menu select
    1. New > Class
  3. In the new dialogue type the name of the any name of the class, in my case "HelloMain" without spacing and quotes.
  4. Now you will see the "default package" under the src folder. default package contains the file you just create.
  5. Type the following code 
    1. 1:  public class HelloMain {  
      2:    
      3:       public static void main(String args[]){  
      4:            System.out.println("Hello World");  
      5:       }  
      6:  }  
  6. You will notice that HelloMain tab in right pane become "*HelloMain". It shows that your file is not saved. Save the file from File menu.
You have complete your first code. Congratulations!!!

Compile and Run

In Eclipse IDE it is very easy to compile and Run the Java program.
Follow these steps:
  1. Select "HelloWorld" project.
  2. Right click on it and find "Run As".
  3. From the sub menu of "Run As" click "Java Application".
  4. You will see the output in console menu.

No comments: