Java Basic Syntax and Hello World Example

Java is a simple and easy to understand programming language, most of its syntax is inherited from c and c++. In Java, classes are the root of each program written, everything goes under a class. Java provides almost all syntactical constructs that a modern programming language should have. Java has classes, methods, variables, arrays, string, event handling, exception handling and much more. In this article we will dive into basic syntax and programming basics of Java programming language.

File name and extension

Java code and classes are written inside a .java class, after comilation a .class file is created. A single code file can have more than one Java class, but there can be a single public class only. If a file contains more than one class than after compilation exact number of .class files are created.

Start of Java program Execution

Every simple java program starts its execution line by line from the main() method, main method has a special syntax and different than other methods.

	public static void main(String args[]){
		// execution starts here 
In the code snipet above, public means the function can be accessed from anywhere, static means this function will execute before anything in that class and void means it does not return anything. It acceps an array of String values, this is used to accept command line arguments. We will see more about them in upcoming articles.


In Java everything goes under a class, classes are nothing but a blueprint or template to represent a object. Classes represents stats and behaviour of an object, object?s states are stored in variables and its behaviour is represented using functions.
// simple sytax to create a class
public class Test {


In the example above, we just created a class with name Test, now all variables and methods will go under its body.


In Java Methods are places which represents behaviour of a class, methods are the places where logics are written, data is manipulated and execution is done. A method may or may not accept parameters to execute some logics on, it may or may not return a value based on execution.
	// simple method syntax, with no parameter and no return value
	public void printName() {

	// simple method syntax, with a single integer parameter and no return value
	public void printName(int id) {


	// simple method syntax, with a single integer parameter and string return
	// value
	public String getName(int id) {
		return "name";
In above code snippet we have seen few basic syntax of methods, but this is not limited there can be more complex examples of methods.


In java there are two type of comments, single line comment and block comments. Single lines are commented using ?//? and block comments are done using ?/*? and ?*/?.
	// this is a single line comment
	 * this is a multi line comment line 1
	 * this is a multi line comment line 2


Java have two types of arrays, single dimentional arrays and multi dimentional arrays. Here are few examples of arrays in Java.
		// arrays example 1
		int [] arr1 = {1,2,3};
		// arrays example 2
		int [] arr2 = new int[3];
		arr2[0] = 1;	
		arr2[1] = 2;
		arr2[2] = 3;
		//multi dimentional  array example
		int [] [] mArr = new int[2][3];


Java Keywords

Like many other programming languages, java has few reserved keywords. These keywords can bot be given to any variable, class or method or any other construct.


Write and run very first java program

1) Open notepad or any other text editor to start writing code in Java.

2) Write a sample program in Java, that will print ?Hello World? to console.

package com.beingjavaguys.example;

public class Hello {
	public static void main(String args[]) {
		System.out.println("Hello World !");

3) Save the file with, in a directory.

4) Compile the Java file with following javac command like:


5) You will notice that a .class file with same name wil be created in the directory.

6) Run the program with following command:

C:\java-programs>java Hello

7) You will see following output on console.

Hello World!

8) Done, you have just started coding in Java.

In this particular article we have seen basic syntax of java, classes, methods, comments, start a program and a basic program to print ?hello world? on console. Java has a lot other useful and advanced stuff like multithreading, exception handling, collections and much more. We will see all of that in coming articles.

About The Author

Nagesh Chauhan

Nagesh Chauhan has 8+ years of software design and development experience in variety of technologies like - Core Java, Java 8 (Streams, Lambda), J2EE (Servlet, JSP), Spring Framework (MVC, IOC, JDBC, SECURITY etc), Spring Boot and Microservices, Kafla, Redis, Cassandra and Spark.