Java Basic Concepts

It is important that you understand the following basic concepts of Java before you start writing a Java Program:

Basic Code Syntax

The code syntax in a programming language is a set of rules for writing code. Java syntax is mostly derived from C and C++ programming language.

In Java, all code is written inside the body of a class. A class body is defined with the opening and closing curly braces { }.

A class is a template for creating objects. A class may contain fields, methods, constructors, and nested classes.


public class <ClassName> {


public class Student {


It is important to note that a Java class name must always match the file name, else the program will fail to compile.

A Java file has .java extension.

Java is case-sensitive

Java is case-sensitive which means that the word message and Message are not same but two different words in Java.


private String message = "Hello! how are you?";
private String Message = "Hey! whats up?";

Semicolon in Java

In Java, a semicolon is used to separate statements and therefore, every statement must end with a semicolon ;.


private String message = "Hello! how are you?";
System.out.println("Message = " + message);

Comments in Java

Comments in Java, are statements that are ignored by the compiler and the interpreter. Comments are basically used to make the code more readable by providing information about a method, variable, class, etc. There are two types of comments in Java:

  1. Single Line comment - It starts with a double forward slash "//".
  2. MultiLine comment - It starts with /* and ends with */.

public class CommentsExample {
  // This is a single line comment.    
   *This is a multiline comment. 
   *Compiler ignores this section of the code.    


Identifier in Java

An identifier is the name used for identifying a package, class, interface, method, or variable in the code. An identifier in Java is case-sensitive. A standard naming conventions should be followed when selecting names. An identifier must be composed of letters, numbers, the underscore _ and the dollar sign $. Identifiers must always begin with a letter, the underscore or a dollar sign.

An identifier should be named in such a way that makes their purpose obvious. Here is an example of some variables that hold the name, profession, and department of an employee.


//The class name Employee is identifier here
public class Employee {

  //The name, profession, and department is identifier here
  String name = "Peter";
  String profession = "programmer";
  String department = "IT";
  //This is a method where main is identifier here
  public static void main(String args []) {
    System.out.printf("My name is %s. I am a %s. My department is %.", 
    name, profession, department);

My name is Peter. I am a programmer. My department is IT.

The Main method

The main() method is the entry point from where the application will start reading the code when the application is run.

Every Java program should have at least one main method. Without the main method, an application will have no entry point. When you run a Java application, the JVM will look for the main method for the application to start. Not all classes need a main method but only one class that serves as an entry point for execution.

Syntax of the main() method

public static void main(String[] args) {
   //The body of the main method.

Here is an explanation of the above Java main method syntax:

  • public — The public keyword makes the main method accessible from everywhere and to every object which may require to use it for launching the application.
  • static — The main method is static because it is called by the Java interpreter before any objects are created. The static keyword allows a method to be called without creating an instance of the class in which the method is defined. Generally, we need to create an object of a class before calling a method of that class but with the use of the static keyword in a method declaration, we can call a method directly without creating an object of the class.
  • void — The void keyword in the method declaration means that the method does not return a value.
  • main — The main is the name of the method.
  • (String[] args) — There is a parenthesis () in every method in Java. The String[] args within the parenthesis of the main method means that the method can take an array of string arguments which can be passed from the command line while starting a program.

Modifier in Java

A modifier in Java, modifies the properties of classes, constructors, methods or variables. There are two types of modifiers in Java:

  1. Access modifiers — The access modifiers are used to restrict the scope of a class, constructor, method, variable or data member. There are four types of access modifiers in Java. They are the default, private, protected, and public.
  2. Here is an example of a method with a public access modifier which can be accessed from anywhere in the program:

        public int substract(int a, int b) {
        int remaining = a - b;
        return remaining;
  3. Non-access modifiers — Java provides a group of non-access modifiers such as final, static, abstract, volatile, transient, synchronized, etc. The use of non-access modifiers gives special properties to variables, methods, and classes. However, they do not change the accessibility of variables, methods, and classes.
  4. Here is an example of a class and a method with the abstract non-access modifier:

        public abstract class Car {
             public abstract double speed();

Keywords in Java

Keywords are reserved words which have predefined functions in Java. Keywords cannot be used as names for classes, variables, methods, or as any other identifier. There are around 57 reserved words in Java.

Here is the list of keywords available for use in Java:

abstract assert boolean break byte
case catch char class continue
default do double else enum
exports extends final finally float
for if implements import instanceof
int interface long module native
new package private protected public
requires return short static strictfp
super switch synchronized this throw
throws transient try void volatile