We shall write write our first Java program using a simple Text Editor like Notepad(in Windows) or gedit(in Linux).
Note:- If you want to learn programming using an IDE, check out the next tutorial.
Step1:- Open your text editor.
Step2:- Click on Save. A pop-up box appears. Create a folder named JavaPrograms under your home folder. Save the file with the name "HelloWorld.java".
Step3:- Start writing the following code.
/*
This is my first Java Code.
*/
class HelloWorld{
//Your program begins with a call to main()
public static void main(String[] args){
System.out.println("Hello World");
} //End of main method
} //End of class
Step4:- Save the file.
Now before running the code, explanation of the code is very important.
Explanation of the code:-
The code begins with the following lines:
/*
This is my first Java Code.
*/
This is a comment. Like other programming languages, Java lets you to add remarks into your source code. The contents of comment are ignored by the compiler. It can be used to explain the operation of the program to anyone who is reading the program.
The next line of code is as follows:
class HelloWorld{
The class keyword is used to declare that a new class is being defined. HelloWorld is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace({) and closing curly brace(}).
The next line in the program is the single-line comment:
//Your program begins with a call to main()
A single line comment starts with a // and ends at the end of the line.
The next line is as follows:
public static void main(String[] args){
This line begins the main() method. As the comment preceding it suggests, this is the line at which program will begin executing. All Java programs begin executing by calling main().
The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. (The opposite of public is private, which prevents a member from being used by code defined outside of its class).
In this case main() must be declared public, since it must be called by code outside of its class when the program is started.
The keyword static allows main() to be called without having to instantiate a particular instance of the class.This is necessary since main() is called by the Java Virtual Machine before any objects are made.
The keyword void simply tells the compiler that main() does not return a value. You will see later, that methods may also return values.
In main(), there is only one parameter. String args[] declares a parameter named args, which is an array of instances of the class String. In this case, args receives any command-line arguments present when the program is executed.
The last character on the line is { . This signals the start of main()'s body.
The next line of code is shown here. Notice that it occurs inside main().
System.out.println("Hello World");
This line outputs the string "Hello World" followed by a new line on the screen. Output is actually accomplished by the built-in println() method. In this case, println() displays the string which is passed to it. As you will see, println() can be used to display other types of information, too. The line begins with System.out.
Here System is a predefined class that provides access to the system, and out is the output stream that is connected to the console.
If all this seems a bit confusing, don't worry. All of these concepts will be discussed in details in later posts.
Keep In Mind:--
- main() is the method called when a Java application begins.
- Java is case-sensitive.Thus Main is different from main. So, if you have typed Main instead of main, the compiler would still compile your program. However java would report an error because it would be unable to find the main() method.
- Class names should start with capital letters.
- All statements should end with a semicolon.
Compiling & Running the Java Program:-
For Windows Users:-
- Go to command prompt.
- Find the actual path for your JavaPrograms folder.
- Type in the command prompt. Type in the full path after cd command.
For example, full path in your Windows7 machine may look like this:-
C:\> cd \JavaPrograms
Now, if you type in dir command, you can see your HelloWorld.java file in the list.
C:\JavaPrograms\> dir
- Next step is to set the path to java folder in your hard drive
C:\JavaPrograms\> set path=%path%;
For example, your JDK path might be C:\Program Files\Java\jdk1.5.0_09\bin
- Next step is to compile the program
C:\JavaPrograms\> javac HelloWorld.java
Now, if you use dir command once again, you will find HelloWorld.class file in the list. That means the
compilation process is complete.
- Next step is to execute the program.
C:\JavaPrograms\> java HelloWorld
- You should see the output "Hello World" in the next line.
For Linux Users:-
- Open Terminal Window.
- You need to navigate the terminal to the folder where your .java program is located.
Type in the following command:-
root@localhost:~$ cd JavaPrograms
- Next step is to compile the program
root@localhost:~/JavaPrograms$ javac HelloWorld.java
Now, if you use dir command once again, you will find HelloWorld.class file in the list. That means the
compilation process is complete.
- Next step is to execute the Java program.
root@localhost:~/JavaPrograms$ java HelloWorld
- You should see the output "Hello World" in the next line.
Now, that completes this lesson. Next, we shall learn about using variables and writing some more programs.
_____________________________________________________________________________________
No comments:
Post a Comment