Understanding The First Java Program

Understanding The First Java Program

Structure of Java File

Source code that we write will be saved using extension .java

  • Every thing written in .java file must be in classes, or we can say that every file having .java extension is a class

  • A class with same name as file name must be present in .java file

  • First alphabet of class name can be in upper case. It is the naming convention of class name. However, it is not compulsory to do so.

  • Class which is having same name as file must be public class

  • A main function/method must be present in this public class, main is a function from where the program starts.


What is package ?

  • It is just a folder in which java files lies.
  • It is used to provide some rules and stuff to our programs.

Hello world program

public class Main{
 public static void main(String [] args){
 System.out.println("Hello World");
 }
}
  • public (in first line):- public is an access modifier which allows accessing the class from anywhere.

  • class :- It is a name group of properties and functions

  • Main :- It is just the name of class as same as the name of file.

  • public (in second line) :- It is used to allow the program to use main function from anywhere.

  • static :- It is a keyword which helps the main method to run without using objects.

  • void :- It is a keyword used when we do not want to return anything from a method/function.

  • main :-It is the name of method.

  • String [] args :- It is a command line argument of string type array.

  • System :- It is a final class defined in java.lang package.

  • out :- It is a variable of PrintStream type which is public and static member field of the System class.

  • println :- It is a method of PrintStream class, It prints the arguments passed to it and adds a new line.

    Note :- print can also be used here, but it prints only arguments passed to it. It does not add a new line.