Saturday 2 March 2013

Hello World in Java

So lets start writing our first java program.Before writing any program one must define what goal the program or in broader sense the project must achieve. This helps us to program better. Generally programmers design UML(Unified modeling language) diagrams like Class Diagram, Sequence diagram etc. before proceeding with coding.We will come to UML diagrams after a few tutorials but lets just write and understand our first program.

In your eclipse dashboard click on go to Workspace.Generally what will load is a default workspace but if you want to have same different workspace you can do File->Switch workspace->other. Lets make a new project now.Go to File->New->JavaProject . Enter a project name and click finish. Don't worry about other parameters for now eclipse will automatically detect installed JDK and JRE.However if it dosen't you may need to manually browse and show it's location.Let the project name be greetings.It is a good programming practice to start your project name with a capital letter.You can now see your project in package explorer tab to the left.If you cannot see go to Windows->ShowView->packageExplorer. It is again a good programming practice to put your code in the new packages you create. Don't use default package.Also it is a convention and good programming practice to start your package name in small letters.So lets create package in our project.3rd  click your project in package explorer and select new->Package. Name it greetingspackage and click finish.You can then see the package in ProjectName/src directory i.e Greeting/src directory.Now go ahead 3rd click on package and select new->Class. Name it Welcome and click finish. Again it is a convention to name starting letter of class name as capital.Now lets start writing code.

Code

package greetingsPackage;
public class Welcome
{
    public static void main(String[] args)
   {
        System.out.println("Hello World!");
   }
}


Understanding the code

First like is package "greetingsPackage;" represents what package this particular file is.Generally this is followed by import lines. Since we are not using any library we need not use any import in this program.
Next line is "public class Welcome " This defines the class name.Remember this name is same as the filename incase you are not using an IDE. Next line is "public static void main(String[] args)". Check out this function very carefully.When we run any java program compiler finds this function and starts execution from here. Remember there is only one main() function in entire project.Now lets analyze this function by breaking it into individual words.
     First word is public. This is an access modifier .There are other access modifiers like private,protected etc but for now understand this that access modifier describe access privileges. Access modifiers will be covered in later tutorials.Next word is static.  Leave this for a moment.Next void is the return type which means function will not return any value. Next main  is the function name followed by parameters ib brackets. String args[] are special parameters taken from command line.args  is just a name and can be replaced by any suitable name.

Final statement is  "System.out.println("Hello World!");" It simply prints "Hello World!" on the standard output. You can view standard output in console at the bottom. If you can't see it again go to Windows->showView->Console.

Snapshot



 If you are compiling and running using command line.Do the following
javac Welcome.java
java Welcome

Output should be printed on command line itself.
If you get error saying "Command not recognized" you have not added JAVA classpath in your environment variables. Or if you don't want to do this run your java program giving absolute path like

c:\Program Files\Java\jdk1.6.0_01\bin\javac Welcome.java
c:\Program Files\Java\jdk1.6.0_01\bin\java Welcome

You can also notice a Welcome.class file generated.It is nothing but the bytecode discussed earlier.

Note: Very important! Java is a case sensitive language so you need to take care to what case you use like for ex if you use string instead of String compiler will throw an error.

Related Links


No comments:

Post a Comment

t> UA-39527780-1 back to top