web analytics

Java basic Syntax

In previous post we created a java file named FirstJavaProgram.java which prints “Hello Word” on screen when we run it using java command¬† after compilation using javac command.


class FirstJavaProgram {

public static void main(String args[]){

System.out.println("Hello World");

}

}

FirstJavaProgram.java explained

In Java every line of code must be inside a class. In above example, we named the class FirstJavaProgram. It is good programming practice to start each word in the class name with an uppercase letter.
Java is case-sensitive therefore “FirstClass” and “firstClass” has different meaning.

Java class name and file name in should be same . When saving the file, save it using the class name and add “.java” to the end of the filename. To run the example above on your computer, make sure that Java is properly installed: Go to the Get Started Chapter for how to install Java. The output should be:

Hello World

Main Method in Java Program


public static void main(String[] args)

Java program execution starts from main method. public, static, void are keywords of java which will be explained later. String[] args are command line arguments which be explained later in detail. For now you should only remember that main method is necessary for every java program with above syntax.

The code line System.out.println(“Hello World”); prints a line of text on command line. You can note a semicolon ( ; ) at the end of the statement. It is mandatory at the end of each java statement. The curly braces {} are used to start and stop a class and method name. You can note starting and ending curly braces of class and main method separately.