Java Hello World Program
-
Upload
javawithus -
Category
Education
-
view
10.899 -
download
1
description
Transcript of Java Hello World Program
The Program
First, let us look at the complete program :
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
The output will be :
Hello World
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
public is an access specifier which states that the class is accessible from any other class
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
class defines a class or in simpler words – a program
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
HelloWorld is the name of the class ( or program ). You can give any other name BUT…
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
… that name should follow the rules of identifiers :1. It should begin with a letter2. Lowercase letters, upper case letters and
numbers can be used. Only $ and _ special characters are allowed.
3. Keywords ( certain reserved words ) cannot be used as identifiers.
4. Spaces are not allowed.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
The opening and closing braces specify the starting and ending of the class.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
Within the class, we have a method.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
The method has a header or method declaration ….
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
… and a body
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
The word public in the method header is the same as the keyword public used for the class. Here, it indicates that other classes can call this method.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
static specifies that we can invoke this method without creating an object of the class.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
void indicates that the method does not return any value.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
main is the name of the method. For a program to be run, the name of the method should always be main.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
With the parentheses are the arguments.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
It takes a String array as an arguments and stores it in the variable args.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
A String is a group of letters. An array is a group of Strings.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
The parameters to main method are passed through command line.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
This line prints the message to the screen. Anything within the quotes is displayed on the screen.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}
System is a predefined class. out is a member of that class and println is a method.
public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Welcome to JavaWithUs.com” ); }}
The above modified program prints
Welcome to JavaWithUs.com
To understand the HelloWorld program in a better way, read the following article :
http://www.javawithus.com/tutorial/the-hello-world-program
Learn Java through the tutorials on our website.http://www.javawithus.com/tutorial/