A Java program follows a definite structure. Even the simplest program shows the main parts: a class, a main method and statements.

A Simple Java Program

Every Java program has at least one class. The program begins at the main method, declared as public static void main(String[] args). public makes main accessible to the JVM, static lets the JVM call it without creating an object, void means that it returns nothing, and String[] args receives command-line arguments. System.out.println() prints a line: System is a class in java.lang, out is its output stream object, and println is a method of that object.

Definition: Main method. The entry point of a Java application, declared as public static void main(String[] args), from which the JVM starts executing the program.

Comments

Comments explain the code and are ignored by the compiler. Java has three kinds: single-line comments, // ..., multi-line comments, / ... /, and documentation comments, /* ... /, which the javadoc tool uses to build HTML documentation.

Use of Math Functions

The Math class in java.lang provides mathematical methods, all static, so they are called with the class name: Math.sqrt(x), Math.pow(x, y), Math.abs(x), Math.max(a, b), Math.min(a, b), Math.round(x), Math.ceil(x), Math.floor(x), Math.sin(x) and Math.random(), with constants Math.PI and Math.E.

Application of Two Classes

A program may have several classes. One class holds the data and methods, and another, containing main, creates its objects and uses them. Only one class in a file may be public, and the file is named after it.

Java Program Structure

A Java source file may contain, in order: a documentation section (comments), a package statement, import statements, interface statements, class definitions, and the main method class. Only the class with main is essential.

Table 1.4: Common Math methods

Method Returns Example
Math.sqrt(x) Square root Math.sqrt(25) = 5.0
Math.pow(x, y) x to the power y Math.pow(2, 10) = 1024.0
Math.abs(x) Absolute value Math.abs(-7) = 7
Math.max(a, b) Larger value Math.max(3, 9) = 9
Math.round(x) Nearest whole number Math.round(4.6) = 5

Example 1.4: A program with two classes

Problem: Write a program with a class Room that computes the area of a room from its length and breadth, and a main class that uses it.

Solution:

  1. Room has fields length and breadth, a method setData() and a method area().
  2. The main class creates a Room object, sets its data and prints the area.

Result: The program below prints the area of a 12.5 by 10 room as 125.0.

public class RoomArea {
    public static void main(String[] args) {
        Room r = new Room();          // object of the first class
        r.setData(12.5, 10);
        System.out.println("Area = " + r.area());
        /* Math functions */
        System.out.println("sqrt(144) = " + Math.sqrt(144));
        System.out.println("2^8 = " + Math.pow(2, 8));
    }
}
class Room {
    double length, breadth;
    void setData(double l, double b) { length = l; breadth = b; }
    double area() { return length * breadth; }
}

Output

Area = 125.0
sqrt(144) = 12.0
2^8 = 256.0

Common mistakes

  • Writing main with a capital M or missing static, so that the JVM cannot find it.
  • Using println with a lower-case system, as in system.out. Java is case-sensitive.
  • Making two classes public in one file.

Key takeaways

  • Execution starts at public static void main(String[] args).
  • Comments are //, / / and /* /.
  • Math methods are static: Math.sqrt(), Math.pow().
  • Only one public class per file, and the file is named after it.

Practice questions

  1. Why is main declared static?
  2. Name the three kinds of comments in Java.
  3. What does Math.pow(3, 2) return?

Answers to the practice questions

  1. So that the JVM can call it without creating an object of the class.
  2. Single-line (//), multi-line (/ /) and documentation (/* /) comments.
  3. 9.0.

Sources

  • Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit I