Java Program to Print Hello World

  • Java Program to Print Hello World

  • The Process of programming in java is divided into 3 phases:
  • 1. Writing Simple Java Program:
  • Create a simple java Program by typing it into a text editor. Then save the program with the same name as that of the class name.
  • 2.Compiling a Java Source File:
  • After Completion of Java program, open Command prompt. Type the Command “javac filename.java” . If their is  any error then remove first.
  • 3. Running a Java Application:
  • To run java Application in the command prompt ,type ” Filename.java” .
  • /* Simple Java program to print “Hello World” */
  • Save the file as ” Welcome.java”
  • import java.io.*;
    import java.lang.*;
    class Welcome
    {
    public static void main(String args[])
    {
    System.out.print("Hello");
    System.out.println("World");
    }
    }

    Output:

  • Hello World

     

  •  Basic Understanding of above Code:
  • 1. import java.io.*;
  • import java.io.*; is a package. Package is nothing but collection of Classes and Interfaces. import java.io is used to import all the classes that are defined in java.io package. io function is used to perform input output operations. “import java.io” package is used read data from a file and write data to a file.

 

  • 2. import java.lang.*;
  • import java.lang.*; is a package. All the Classes in the java.lang Packages are imported by default. You don’ t need to import java.lang package in every program.

 

  • 3. class Welcome {
  • The class declared using class keyword. Welcome is the name of the class. The entire class defination, including all of its members, will be between Opening curly braces and the closing curly braces.

 

  • 4. public static void main(String args[])
  • Every java applications must begin with main method. Above line contains a number of Keywords such as public, static and void.
  • The public keyword is an access specifier, which means the method is accessible by any class in any package. The public keyword can be accessed from outside by Java virtual Machine (JVM).
  • The Static keyword allow main() to be invoked without creating any object of the class. Static method can be invoked from the class reference.
  • The keyword void indicate that main() does not return any value.
  • The main() is the method called when a Java application starts. This is the starting point for the interpreter to begin the execution of the java program.
  • String args[] declares a parameter names args, which is an array of instances of the class type String.
  • 5.  {
  • The Opening curly brace indicate the start of main() body which include methods and variables.

 

  • 6. System.out.print(“Hello”);
  • System is a predefined class that provide access to the system . Out is the output stream connected to the console. The print() is a built-in method used to display the string. This string is not followed by a newline. The above line display the output “Hello”. The cursor will be on the same line after “Hello”.

 

  • 7. System.out.println(“World”);
  • System is a predefined class that provide access to the system . Out is the output stream connected to the console. The println() is a built-in method used to display the string. This string is followed by a newline. The above line display the output “World”. The cursor move to next line.
  • The first  Closing brace } in the program ends the main() method and the last Closing brace } in the program ends the Class defintion ( Welcome ).
  • Compiling java program:

    Any Java program is first Compiled by the java Compiler. Interpreter and Compiler both are written in high level programming language and they are translated into machine code.

  • To Compile the Program, we must run the java Compiler javac, with the source file name on the command prompt:
  •                       javac filename.java
  • After Compilation of Java program we get .class file containing the bytecode and this file is interpreted by java interpreter known as Java Virtual Machine (JVM). The Compiler automatically name the byte code file as
  •                      filename.class
  •                   Example: Welcome.class
  • Exercises

Exercise 1:

import java.io.*;
import java.lang.*;
class Welcome
{
public static void main(String args[])
{
System.out.println("Hello");
System.out.println("World");
}
}

 

Exercise 2:

import java.io.*;
import java.lang.*;
class Welcome
{
public static void main(String args[])
{
System.out.print("Hello");
System.out.print("World");
}
}

Exercise 3:

import java.io.*;
import java.lang.*;
class Welcome
{
public static void main(String args[])
{
System.out.println("Hello");
System.out.print("World");
}
}

Exercise 4:

import java.io.*;
import java.lang.*;
class Welcome
{
public static void main(String args[])
{
System.out.println("Hello");
System.out.print("Java");
}
}

 

                Output for Exercise 1:

Leave a Comment