Java Hello World Program

23
Java Hello World Program By JavaWithUs ( www.javawithus.com )

description

This presentation explains the 'Hello World' program in Java.

Transcript of Java Hello World Program

Page 1: Java Hello World Program

Java Hello World Program

By JavaWithUs ( www.javawithus.com )

Page 2: 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

Page 3: Java Hello World Program

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

Page 4: Java Hello World Program

public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}

class defines a class or in simpler words – a program

Page 5: Java Hello World 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…

Page 6: Java Hello World Program

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.

Page 7: Java Hello World Program

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.

Page 8: Java Hello World Program

public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}

Within the class, we have a method.

Page 9: Java Hello World Program

public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}

The method has a header or method declaration ….

Page 10: Java Hello World Program

public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}

… and a body

Page 11: Java Hello World Program

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.

Page 12: Java Hello World Program

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.

Page 13: Java Hello World Program

public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}

void indicates that the method does not return any value.

Page 14: Java Hello World Program

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.

Page 15: Java Hello World Program

public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}

With the parentheses are the arguments.

Page 16: Java Hello World Program

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.

Page 17: Java Hello World Program

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.

Page 18: Java Hello World Program

public class HelloWorld { public static void main ( String[] args ) { System.out.println ( “Hello World” ); }}

The parameters to main method are passed through command line.

Page 19: Java Hello World Program

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.

Page 20: Java Hello World Program

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.

Page 21: Java Hello World Program

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

Page 22: Java Hello World Program

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/

Page 23: Java Hello World Program

Presentation by

www.javawithus.com