Ticker

6/recent/ticker-posts

Java (Java hello world program)

JAVA HELLO WORLD PROGRAM

In this article we will learn, how to write first Java program?
So lets start it ...





Step 1. Open the notepad text editor, and the write the following code.


class Hello
{
static public void main(String as[])
{
System.out.println ("My first Java program");
}
}


Step 2. Now save the file with Hello.java file name in D:\JavaPrograms.

Step 3: Open command prompt (MS-DOS) and go to the directory where we saved out first java program (D:\JavaPrograms)

Step 4: Type javac Hello.java and press the Enter key to compile the Java program.
This command will call the Java Compiler to compile the specified file and, if there are no errors in the code, the command prompt will take us to the next line.

Step 5: Now type java Hello on command prompt to run our first Java program and get the following program output.




PROGRAM EXPLANATION

class : The class keyword is used to declare the classes in Java.

public : It is an access specifier in Java and public access specifier is used to declare public members (like class and functions) and these public members is visible to all the classes.

static : The static is also a a keyword in Java and it is used to make a function static. 
To execute a static function we don't need to create any object of the class because the static functions like main() method/function is automatically called by JVM.

void : It is the return type for a function and its meaning, function will not return any value.

main : The main() method is the most important method in a Java program because this method execute at first in any Java program, therefore all the logic must be written inside the main() method. 

Note: If a java class don't have a main() method then it causes to compilation error.


String[] args : This statement is used to represents an array in Java, whose type is String and name is args.

System.out.println : This statement is used to print anything on the console (output) window like printf in C and cout in C ++ language.

















Post a Comment

0 Comments