Define / Explain / Components a Java Program.

1. Define a Java Program / Explain the basic format of a Java Program.

or

2. Explain the Basic  terminologies of a Java Program

When we consider a Java program, it can be defined as a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what do class, object, methods, and instance variables mean.

  • Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behavior such as wagging their tail, barking, eating. An object is an instance of a class.

  • Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type supports.

  • Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.

  • Instance Variables − Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.

A Java program consists of the following basic components:

  1. Comments in a program — Comments are non-executable statements included to provide notes about the program. They are most commonly used to explain the logic of the program. Comments are ignored by the compiler.
  2. Declaration of class — All Java programs start with a class. The syntax of class declaration is class { /*class body*/ }
  3. Declaration of main function — The main method is the main entry point to the program. Program execution begins with the main method. The syntax of main method declaration is public static void main(String args[]) { /*body of main method*/ }
  4. The statements of a Java program are terminated with a semi-colon. Missing a semi-colon will lead to compilation errors.

Below is an example of a basic Java program:

/*
 * Example of a basic Java program
 */

//Class Declaration
public class Add 
{
    // Main Declaration
    public static void main(String args[]) {
        int a = 10, b = 15, c = 0;
        c = a + b;
        System.out.println("Sum = " + c);  
    }
}

No comments:

Post a Comment