Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Java Syntax and Basic Structure

3. Java Syntax and Basic Structure

Java is known for its clear and consistent syntax. Understanding the syntax and basic structure is crucial when writing Java programs.

Java Program Structure:

A Java program is organized into classes. Each class serves as a blueprint for objects. Here's the basic structure of a Java program: 
public class ClassName {
    // Class variables (also known as fields)
    DataType variableName;

    // Constructor
    public ClassName() {
        // Constructor code here
    }

    // Methods
    public ReturnType methodName(ParameterType parameterName) {
        // Method code here
    }

    public static void main(String[] args) {
        // Main method code here
    }
}

 

Let's break down these key elements:

  • 'public class ClassName { ... }': This defines a class named 'ClassName'. The class name should match the filename (with a  '.java' extension) where it's defined. In Java, class names conventionally start with an uppercase letter.

  • Class Variables: Variables declared within the class are called class variables or fields. They store data related to the class. For example,  'DataType variableName;'  declares a class variable.

  • Constructor: The constructor is a special method used for initializing objects of the class. It has the same name as the class and no return type. It's called when an object of the class is created. For example, 'public ClassName() { ... }'  is a constructor.

  • Methods: Methods are functions that perform actions or calculations within a class. They have a return type (or  'void' if no value is returned), a name, and can accept parameters. For example, 'public ReturnType methodName(ParameterType parameterName) { ... }' is a method.

  • 'public static void main(String[] args)': This is the main method, which serves as the entry point of the program. It's a special method where program execution begins. The 'String[] args'parameter allows you to pass command-line arguments to your program. 

Java Statements and Blocks:

In Java, statements are individual instructions that make up a program. Statements typically end with a semicolon ( ';'). Here are some common types of statements:

1. Variable Declaration and Initialization:
int age; // Declaration
age = 25; // Initialization

 

2. Conditional Statements (if-else):
if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}

 

3. Loops (for, while, do-while):
for (int i = 0; i < 5; i++) {
    // Loop body
}

while (condition) {
    // Loop body
}

 

4. Method Invocation:
int result = methodName(argument1, argument2);

 

5.  Assignment Statements:
variable = expression;

 

6. Print Statements (for output):
System.out.println("Hello, World!");

 

Example Java Program:

Let's create a simple Java program that calculates the sum of two numbers:
public class Calculator {
    // Class variables
    int num1;
    int num2;

    // Constructor
    public Calculator(int a, int b) {
        num1 = a;
        num2 = b;
    }

    // Method to calculate the sum
    public int calculateSum() {
        return num1 + num2;
    }

    public static void main(String[] args) {
        // Create an instance of Calculator
        Calculator calculator = new Calculator(10, 5);

        // Calculate and print the sum
        int sum = calculator.calculateSum();
        System.out.println("Sum: " + sum);
    }
}

 

In this example, we've defined a class 'Calculator' with class variables  'num1' and 'num2' , a constructor to initialize them, and a method  'calculateSum' to perform the calculation. The  'main' method is where the program starts, and it creates an instance of 'Calculator' to calculate and print the sum.

Understanding these core Java syntax and structure concepts is essential for writing Java programs effectively.