Java Tutorial


OOPS concept


Collection

Java Basic Syntax

Hello world Program

class HelloWorld
{
  public static void main(String[] args) 
  {
     System.out.println ("Hello World!!");
  }
}
Lets start with simple Hello World program.
  • Open you notepad or any other text editor you like.

  • Copy this code and save the file with name HelloWorld.java

  • open your command propmpt (cmd). Change you directory where you have saved your file.

  • Suppose you have saved your file in C:\

  • Now type javac HelloWorld.java and press enter. This will compile the program. If there are no errors in the code the command prompt will take you to the next line.

  • Now type java HelloWorld to run your program

  • you will see output on command propmp as Hello World!!

Understanding code


class

This is a keyword which is used to declare a class in java.


public

This is a keyword, an access specifier used to represent that this function is visible to all.


static

It is a keyword used to make static method. Advantage is that we do not require an object to access this method. JVM call main() method without creating any object.


void

It is a return type. Means function is not returning any value.


main

Execution of the program starts from the main() method, therefore if it is missing, there will be a compilation error.


System.out.println()

Used to print statement written inside it. Like cout in C++ and printf() in c.

Advertisment

Share


Advertisement