Pre Course Home Next

Java Tutorial

Basic Syntax

In this section we'll see the basic syntax of java language. We'll first create a simple java program which will print "Hello from Tutoz." in the console. Create a new file with name Hello.java and paste the following code in the file.

public class Hello {

	public static void main(String[] args) {
		// this will print Hello from Tutoz.
		System.out.println("Hello from Tutoz!");
	}
}

Name of class and name of file name mush match. Now open your console, navigate to the location through command line where you have saved this file and type

javac Hello.java.

You must see "Hello from Tutoz!" printing on the console.

Now let's see the the details. In the beginning of the program, you can see the public keyword. This public class specifies that any one can access this class. In java these types of keywords are called access modifiers

Second keyword is class and after that the class name. class keyword is used to define a class. Program file name and class name must match. Like in this example class name is Hello and source file name is Hello.java. Body of the class starts with a left curly brace {  just after the class name and ends with a right curly brace }.

A class contains fields properties and methods. Methods are the function who performs some computation and produces the result. Here in this Hello class, it has a method named main.

Pre Course Home Next