The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Basic Java Syntax

Java Program Structure
Let's look at most basic Java program :



/*
 * File : FirstProgram.java
 * Author : csegeek
 * Description : My first Java Program
 */
class FirstProgram {
   // program execution begins at main
    public static void main(String args[]) {
       System.out.println("Hello World"); // prints Hello World
    }
}

The program must be saved as FirstProgram.java. Then compile and run.
Output : Hello World

Comments
Comments are a way of explaining what your program does. They can be single line ( put after // ) or multi line
( enclosed within /*      */ ). Comments are ignored by the compiler and are just a means for you and others to understand the piece of code.

Class Declaration
Since Java is a true object oriented language, everything must be defined inside a class. In the above program, we create a class named FirstProgram.

main ( )
The program execution begins at the main ( ) method.
void specifies the return type of main ( ) method. void means that no value is returned. Every program must have one and only one main ( ) function.
public is an access modifier which specifies the visibility of the method. We will learn about various access modifiers later but for the time being, just remember that public means visible to all other classes.
static keyword makes a method static i.e we don't have to create object to invoke the static method. Here static means that the method belongs to the class and it doesn't become a part of objects (if created) of this class. main( ) must be static because it is called before any object is created. Please note that, in the above program, we didn't create any object. The main ( ) method is invoked by the JVM itself as it is the starting point of execution.
String args[ ] is used to take command line arguments i.e the main( ) can take any number of parameters from the command line where each parameter / argument is a string. We will see an example later.

System.out.println ( ) is used to print Hello World on the screen. Here, println( ) is a method which appends a newline character to the string to be printed. This method is a member of out object and out is again a static data member of System Class.

Creating Objects
Let's see an example of creating objects and accessing methods :

/*
 * This program demonstrates the creation of objects
 */
public class ObjectDemo {

   // A method to print a line on the screen
   public void printInfo() {
      System.out.println("printfInfo() : I am called by an object");
   }

   public static void main(String []args) {
      ObjectDemo obj = new ObjectDemo(); // Object creation
      System.out.println("main() : Calling printfInfo()");
      obj.printInfo(); // calls the method
   }
}


Java Program with Two Classes
We will now look at a more practical Java program in which we use 2 classes.

/*
 * This program demonstrates a Java Program with 2 classes
 */
class UtilityClass {
   public void method1() {
      System.out.println("Method 1 called");
   }
   public void method2() {
      System.out.println("Method 2 called");
   }
}

public class Main {
   // main method
   public static void main(String []args) {
      UtilityClass uobj = new UtilityClass(); // Object creation
      uobj.method1(); // calls method 1
      uobj.method2(); // calls method 2
   }
}

Please note that the above program has to saved as Main.java because Main is the class which contains
main( ) method. Also, we didn't declare UtilityClass class as public because any class with public visibility has to saved in in a file with the same name as that of the class. So, in this case, Main is the public class and is saved in a file Main.java.

Back | Next