0% found this document useful (0 votes)
5 views

2) First Java Program

This document provides step-by-step instructions for creating and running a simple Java program, 'HelloWorld', using both command prompt and an IDE (Eclipse). It explains the necessary setup, including creating a package, setting the JDK path, compiling, and executing the program, as well as detailing the significance of keywords and structure in the Java code. The document emphasizes the importance of access specifiers, class declaration, and the main function in Java programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2) First Java Program

This document provides step-by-step instructions for creating and running a simple Java program, 'HelloWorld', using both command prompt and an IDE (Eclipse). It explains the necessary setup, including creating a package, setting the JDK path, compiling, and executing the program, as well as detailing the significance of keywords and structure in the Java code. The document emphasizes the importance of access specifiers, class declaration, and the main function in Java programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

FIRST JAVA PROGRAM:

CREATE USING COMMAND PROMPT:

1) Create a new Package (folder) in the required location


2) Open folder -> right click -> New -> Text document -> Open -> Save As ->
HelloWorld.java and choose Save As Type as All Files and click OK
3) Open the file, type the following and save:

public class HelloWorld {

public static void main(String args[]) {

System.out.println("Hello World!!");

}
}

4) Set path of the JDK :

i) Choose My Computer -> Right Click -> Properties -> Advanced Tab ->
Environment Variables -> System Variables -> choose PATH and click EDIT
ii) Variable Name will be there as PATH and in the Variable Value, add a
semicolon and give the path for the bin folder of the JDK:

;C:\Program Files\Java\jdk1.6.0_21\bin

iii) If PATH variable is not there, click NEW and specify Variable Name as PATH
and Variable Value as C:\Program Files\Java\jdk1.6.0_21\bin
iv) Click OK to close all windows.

5) Open Start -> Run -> type “cmd”


6) Move to the location of the created Java file.
7) Type for compilation
javac HelloWorld.java
8) Type for Execution
java HelloWorld
9) Output is displayed.
CREATE USING IDE:

1) Open Eclipse IDE


2) Open File -> New Project -> Java Project
3) In Package Explorer, right click Project folder -> New -> Class
4) Specify the Class Name and click Finish
5) Type the following:

public class HelloWorld {

public static void main(String args[]) {

System.out.println("Hello World!!");

}
}

6) Right click the project and choose Run As -> Java Application
7) Output is displayed in Console Window in bottom.

EXPLANATION OF SIMPLE PROGRAM IN DETAIL:

1) public - keyword:
It is the Access Specifier. When a class is public, it can be accessed by any other class.

2) class – keyword:
It is used to declare as a class and can be instantiated with objects or can be inherited
by another class.

3) Class name – (HelloWorld):


Class name and File name must be same. Class name must begin with
alphabet/underscore (_) / dollar ($) and can be followed by numbers/alphabets/
underscore (_) / dollar ($).

4) main function – starting point of execution of program:


i) public - specifies that main function is accessible by any other function in other
classes.
ii) static – specifies that main function can be called without creating objects.
iii) void – specifies that main function will not return any values
iv) String args[] – command line argument which is a string array. This allows
main method to be invoked with array of string objects during the time of
execution.
v) System.out.println() – is used to print the data where data must be given
within double quotes.

You might also like