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.
Note:
- 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.
Firstly type "java" leave a space and mention the filename
as it is .
Remember Java is case-sensitive.
The following picture shows the difference between the command to be written for compiling
and running the program. Do not get confused between them.
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!!!
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 .
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).
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.
program we will use the very basic editor notepad.
Following is the
code for this program.
|
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.
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.
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
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.
_________________________________________________________________________________
FOLLOWING ARE THE STEPS INVOLVED :
Step 1 :- Write the
program in Java.
- 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.
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.
shown below.
(Note: Please make
sure you have copied the above code properly and saved as given above
in the step 1 . )
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.
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.
now we can happily run our program.
Remember Java is case-sensitive.
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.
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.
Hello World - First Java Program
Reviewed by shashank
on
00:21
Rating:
No comments: