Java SE 8 and JDK 8 Launched.For Download:Click Here!


Social Icons


Friday, 24 January 2014

Hello World - First Java Program

      Hello World      We have learnt many things about Java in theory. Now , it's time to write our first actual application or a program. It’s traditional or say conventional to start learning a new programming language by writing a program called "Hello World". You can think of it as a very simple initiation into the ranks of Java programmers. All the program does is write the text "Hello World" to your computer screen.
Checklist :
To write your first program, you'll need:
1.The Java SE Development Kit 7 (JDK 7)
You should have a properly installed and fully running Java environment .
If not done , Click Here.

2.A text editor
In this example, we'll use Notepad, a simple editor included with the Windows platforms. You
can easily adapt these instructions if you use a different text editor(In further tutorials we will
use different editors also).
To know more about Text Editor, Click Here.
    * These two items are all you'll need to write your first application.

The basic steps we will follow for our Hello World program are:
1.Write the program in Java
2.Compile the source code
3.Run the program

To start writing our program we need a place to write it. For our first and the most simplest
program we will use the very basic editor notepad.
Following is the code for this program.

class MyFirstJavaProgram
      /* This is my first java program. 
      * This will print 'Hello World' as the output
            public static void main(String args[])
                    System.out.println("Hello World"); // Prints Hello World

 You could cut and paste the above code into your text editor (in our case it's notepad) ,but 
it's better to get into the habit of typing it in. Writing will give you the feeling of how to 
write the programs.
Just remember , copy exactly the same code as it is given above , otherwise some errors
will occurs.

As it is the first program , I will not explain the code or any terms involved in it  ; that
all will be covered up in the later posts. At present , it's more important for you to experience
the steps involved in creating and running the program.
For understanding those terms :- Click Here 

Be Careful when you type A or a . Because Java is case-sensitive.
E.g.  MyFirstJavaProgram is not same as the myfirstjavaprogram.

Step 1 :- Write the program in Java.

 -Open Notepad and copy the code given above or type it.

    - Save the file with the name same as the class name. In our case , it is MyFirstJavaProgram.
 And please note save the file with the .java extension as shown below , because it tells the
 computer that our file is a java file.

  - Open the command prompt.

 -After opening the command prompt , Type " cd/ " to come out of all the subfolders.

 -Type "cd" then put a space and mention the path of your file as done below.

Step 2 :- Compile the source code.

-As we have already learnt how to set the environment variables in our previous post ,now
 we can  directly use javac as shown below.
 Firstly type "javac" then leave a space and write your filename with the java extension as
 shown below.
 (Note: Please make sure you have copied the above code properly and saved as given above 
 in the  step 1 . )

If everything is right , nothing will pop up on the command prompt like error or exception.
  If some error or exception comes up
   * Please go again through this post and do exactly the same things as mentioned .
   * And also check whether your java is correctly installed . For that : Click here.

Compiling Hello World

Step 3 :- Run the program.

-As we have compiled are program successfully , it ensures there are no compile time errors. So
 now we can happily run our program.
   Firstly type "java" leave a space and mention the filename as it is .
   Remember Java is case-sensitive.

running hello world 

Hello World has been printed.
Congratulations! Your program works!

The following picture shows the difference between the command to be written for compiling
and running the program. Do not get confused between them.

compile and run

I'll admit that it's not the most exciting program ever written. Don't get disheartened if you
didn't understand a single word in this program . For the time being , it is better for you to just
know how to create a program , compile it and run it. Everything else will be discussed in the upcoming posts. For understanding those terms :- Click Here . More Complex programs are coming up. Stay tuned to JavaCompile.

Keep Compiling!!! 
Comment With:
The Choice is Yours!

No comments:

Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Follow JavaCompile by Email

Related Posts Plugin for WordPress, Blogger...

Total Pageviews


Blogger Widgets