KnowledgeBoat Logo
|
LoginJOIN NOW

Computer Applications

What is the basic format of a Java Program? Explain with an example.

Java Intro

215 Likes

Answer

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);  
    }
}

Answered By

134 Likes


Related Questions