Java – Basic Syntax

We a are going to examine the basic syntax of the Java programming language. Just like you already know, Java is an object oriented programming(OOP) language. As a result, we can consider a Java program as a collection of objects that can communicate with each other. Communication is done using the methods of these objects.

Table of Content

  1. OOP Terms
  2. Write Your First Java Program
  3. Naming Rules in Java
  4. Java Identifiers

1. OOP Terms

First, you need to understand some basic terms used in a Java program. We would explain some of them.

Class: An class is a blueprint or template from which objects can be created. A such, a class contains a specification of attributes(variables) of an object and actions(methods) the object can perform.

Object: An object is a representation of real-world objects. An object is what is created from a class. Therefore an object is an instance of a class. For example, you can create a class that represents vehicles. Then later, you can create a vehicle object that has a name, model, make, color and other attributes.

Method: A method is an action an object can take. You can say it is the behavior of the object. Classes contains method. Methods are written as functions in a class.

Attributes: An attribute is is characteristic of a class. It is represented in a class a variables or (class variables). We would talk about class variables later.

2. Write Your First Java Program

We would not just write a hello world program! We are going write something that is a little better that would help you better understand Java Basic Syntax. Let’s write a program that:

  • prompts the user for his name
  • gets the input from the user
  • display a greeting to the user

The program is given in Listing 1.0. Copy it and paste it in your IDE. Save it with a the filename, FirstProgram.java. Then run it

import java.util.Scanner;

//Your First Program
public class FirstProgram {
	public static void main(String[] args) {	
		System.out.println("Please enter your name: ");
		Scanner scanner = new Scanner(System.in);
		String name;
		name = scanner.next();
		System.out.println("Welcome " + name);
	}
}

Listing 1.0: Your First Program

If you run it correctly, then your output would be similar to the output in Figure 1.0.

Figure 1.0: Your First Java Program

Let’s now try to understand this program line by line

Line 1: Import java.util.Scanner

This line tell the compiler that a function in java.util.Scanner package would be used in the program

Line 3: //Your First Java Program

This is a single-line comment. Comments are not executed by the compiler. Its just statements you add to explain your codes

Line 4: Public class FirstProgram {

This line specifies the name of the file or class. This is also the name of the program

Line 5: Public static void main(String[] args) {

This is the main method. The main method must appear in every java program. It is the entry point of the program

Lines 6 to 10

This is the body of the main method. These are the statements that is executed when the program is run. We would understand more about this statements as we move along.

3. Naming Rules in Java

The following rules represent the Java Basic Syntax. Therefore, they must be strictly followed.

Case Sensitivity: The Java Language is case sensitive. As such, two similar words but different cases would have different meanings

Class Names: Class names in Java should always start with an upper case letter.

Method Names: Method names should always begin with a lowercase letter.

File Name: The filename of a java program must be exactly the same as the name of the class.

4. Identifiers in Java

Identifiers are name you choose to used in your program. You may use such names for variables, classes and methods. The following rules apply to naming an identifier in java.

  • Identifiers must begin with an a letter, currency symbol($) or an underscore
  • Identifiers are case sensitive
  • Can contain any other characters after the first character including numbers
  • A keyword cannot be used as identifier

Watch the Video