The Tutorial-Wizards Easiest Tutorials On The Web

Java Basic – Hello World using notepad

Title: Hello World in java using notepad

Aim: To write to run and to understand the first program in java (Hello World program)

Requirements: java compiler, notepad or any other text editor

Steps:

I. Prerequisites
II. How to write program?
III. How to compile the program?
IV. How to run the program?
V. Understanding the Code


I. Prerequisites:

a. Check if the compiler is ready

Open Command prompt
Click start>Run and type cmd

Run Box

b. Type javac
If you see the text as shown in the screenshot below then your compiler is installed properly and the environment variable is set.

cmd comand 'javac'

If you see text as shown in the screenshot below then environment variable is not set.
Refer to http://tutorial-wizards.com/java/java-basic-installing-java-compiler to set the environment variables.

Javac command fail

II. How to write the program?

1. Open any text editor like notepad/notepad++
I recommend using notepad++ as it provides support for color coding and indentations (It’s free).
Notepad++ download page: http://notepad-plus-plus.org/download/v5.9.6.2.html
For this tutorial we will use notepad
To open notepad click start > Run and type notepad press enter

run notepad

2. Write following piece of code in notepad
/**
* The HelloWorld class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}

3. Save the program with the same name as that of class name i.e. HelloWorld.java
Click on file > Save as
Then type HelloWorld.java as the filename
We will save our file in ‘C:/JavaPrograms’ directory
Refer screenshot below

save screen

III. How to compile the program

1. Open command prompt
Click start>run and type cmd in run dialog box
It should show command prompt as below

cmd

2. Change directory (using cd) to the directory where java file is saved.

cmd cd

3. Type following command to compile java program
javac
In our example type following command
javac HelloWorld.java

cmd cd

If you see following text then environment variable is not set
Refer http://tutorial-wizards.com/java/java-basic-installing-java-compiler to set the environment variables.

cmd java fail

If everything is fine you should be able to execute the command without any errors.
You can check if your code is compiled by browsing the directory where the .java file was saved.
A Class file with the same name having .class extension should have been created.

file .class

IV. How to Run the program

1. Open command prompt
2. Change directory (using cd) to the directory where java file is saved.
3. Type following command to run the program
java
In our example
java HelloWorld
4. Following output should be generated

cmd sucess

V. Understanding the Code

The "Hello World!" application consists of three primary components:
• source code
• comments
• the HelloWorld class definition
• and the main method
The following explanation will provide you with a basic understanding of the code, but the deeper implications will only become apparent after you've finished reading the rest of the tutorial.

Source Code Comments

The following bold text defines the comments of the "Hello World!" application:
/**
* The HelloWorld class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Comments are ignored by the compiler but are useful to other programmers. The Java programming language supports three kinds of comments:
/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /*and */. The javadoc tool uses doc comments when preparing automatically generated documentation. For more information on javadoc, see theJavadoc™ tool documentation .
// text
The compiler ignores everything from // to the end of the line.

The HelloWorld Class Definition

The following bold text begins the class definition block for the "Hello World!" application:
/**
* The HelloWorld class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
As shown above, the most basic form of a class definition is:
class name {
. . .
}
The keyword class begins the class definition for a class named name, and the code for each class appears between the opening and closing curly braces marked in bold above.

The main Method

The following bold text begins the definition of the main method:
/**
* The HelloWorld class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
In the Java programming language, every application must contain a main method whose signature is:
public static void main(String[] args)
The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".
The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.
The main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to your application. For example:
java MyApp arg1 arg2
Each string in the array is called a command-line argument. Command-line arguments let users affect the operation of the application without recompiling it. For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument:
-descending
The "Hello World!" application ignores its command-line arguments, but you should be aware of the fact that such arguments do exist.
Finally, the line:
System.out.println("Hello World!");
uses the System class from the core library to print the "Hello World!" message to standard output. Portions of this library (also known as the "Application Programming Interface", or "API")

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.