Hello world program in Java
Before we start learning about program, if you are not aware of what is Java check out my previous article Introduction to Java
Hello World Program
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}
Lets see , what is meaning of this program and how it can be saved ,compiled and run:
- In java every line of code that can actually run should be inside class.
- To run any java program main method is mandatory and will be defined in the same format :
public static void main(String []args)
public : can be accessed from anyone within the system
Static : using static keyword ,any method can be run ,even without main method.
void : It doesn’t have any return type.
main :the name of the method
String[] args : array of string called args.
Now coming to the body of the method ,which is enclosed with curly braces.
{
System.out.println("Hello World");
}
{}: Method is enclosed with curly braces which indicates the start and end of the method
System: System is a class in java.lang package.
Out :out is a static member of the System class, and is an instance of java.io.PrintStream
Println: "println" is a method of java.io.PrintStream which prints the line of text on screen.
; : semicolon is a terminator symbol for statement which represents end of statement
Comments
Post a Comment