


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
First Program in Java, HelloWorldApp, Compiling and Running HelloWorldApp, String args[], Public static void main, Line of code, Compiling and executing, Two braces. Virtual University is one of best in Pakistan for distance education in science.
Typology: Lecture notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Web Design & Development CS-
Like any other programming language, the java programming language is used to create applications. So, we start from building a classical “Hello World” application, which is generally used as the first program for learning any new language.
Note: Don’t copy paste the given below code. Probably it gives errors and you can’t able to remove them at the beginning stage.
Note: Name of file must match the name of the public class in the file (at line 4). Moreover, it is case sensitive. For example, if your class name is MyClasS, than file name must be MyClasS. Otherwise the Java compiler will refuse to compile the program.
For the rest of this handout, we assume that program is saved in D:\examples directory.
Web Design & Development CS-
HelloWorldApp Described
Lines 1- Like in C++, You can add multiple line comments that are ignored by the compiler.
Lines 4 Line 4 declares the class name as HelloWorldApp. In java, every line of code must reside inside class. This is also the name of our program (HelloWorldApp.java). The compiler creates the HelloWorldApp.class if this program successfully gets compiled.
Lines 5 Line 5 is where the program execution starts. The java interpreter must find this defined exactly as given or it will refuse to run the program. (However you can change the name of parameter that is passed to main. i.e. you can write String[] argv or String[] someParam instead of String[] args)
Other programming languages, notably C++ also use the main( ) declaration as the starting point for execution. However the main function in C++ is global and reside outside of all classes where as in Java the main function must reside inside a class. In java there are no global variables or functions. The various parts of this main function declaration will be covered at the end of this handout.
Lines 6 Again like C++, you can also add single line comment
Lines 7 Line 7 illustrates the method call. The println( ) method is used to print something on the console. In this example println( ) method takes a string argument and writes it to the standard output i.e. console.
Lines 8- Line 8-9 of the program, the two braces, close the method main( ) and the class HelloWorldApp respectively.
Web Design & Development CS-
An Idiom Explained
You will see the following line of code often:
“main” is the function from which your program starts
Why public?
Since main method is called by the JVM that is why it is kept public so that it is accessible from outside. Remember private methods are only accessible from within the class
Why static?
Every Java program starts when the JRE (Java Run Time Environment) calls the main method of that program. If main is not static then the JRE have to create an object of the class in which main method is present and call the main method on that object (In OOP based languages method are called using the name of object if they are not static). It is made static so that the JRE can call it without creating an object. Also to ensure that there is only one copy of the main method per class
Why void?
What is String args[]?
Way of specifying input (often called command-line arguments) at startup of application. More on it latter