Structure of Java Program

In today’s topics, we will discuss the basic structure of Java program. The various terms and concepts which are used to write a java program we will discuss that. Previously we also describe briefly various topics of Java-like Features of Java, Architecture of Java, History of Java, Difference between C++ & Java, How to install Java, etc, and many more. So without wasting the time let’s dive into the topic:

Structure of Java Program

There are various concepts used in a java program i.e.:

  • Classes
  • Objects
  • Packages
  • Methods
  • Interface
  • OOPs Concept
  • Exception Handling
  • Multi-Threading

Classes

A class is a grouping of objects or collections of objects having identical properties, common behavior, and shared relationship. Here all the class objects have basic class properties. Or you can directly say that a class is a logical template that is used to create objects.

To create a class we have to use the ‘class’ keyword. The first letter of a class name should always start with an uppercase letter and we have to also check the name of the Java file name should match the class name.

A class is declared in the following format:

class  [Class_name] {
//statements…..
//class body
//statements……
}

Example: 

class GeektoCode {
		//class body
		}

Objects

The object is the runtime entity in the object-oriented system. We generally say that an object is an instance of a class. Every object has its own properties and features. We also say that an object is a specimen of the class.

We all know that in java, an object is created from a particular class, we have already created a class named GeektoCode, so now we can use this to create objects.

To create an object of GeektoCode, we have to specify directly the class name and then define the object name by using the keyword ‘new’.

Example:

class GeektoCode{
    public static void main(String[] args){
        GeektoCode obj = new GeektoCode();
    }
}

Packages

A package in Java is a namespace that is organized as a set of similar types of classes, interfaces, and sub-packages. It is very simple to understand that it is similar to various folders that are organized on our computer within that folder various types of files and documents are available.

The package helps us to manage the code by preventing naming conflicts. Java provides some built-in packages but still, we can also create our own packages. So among these packages are categorized into two types i.e.

  • Built-in package
  • User-define package

To use a package simply write at the top of the program by using the ‘package’ keyword and mention the package name i.e.

               package com. company;

Methods

A method is a self-contained block of statements or a sub-program of one or more statements that performs a special task when it is called. We can pass data into the data method which is known as a parameter. This parameter has the capability to hold some value. The method is used to perform certain actions for some specific task and they are also known as functions.

Why do we use a method? : To reuse the code, means we can define the code once and use it multiple times as per our requirement.

A method is a function which is must be declared or written within a class. And it is defined with the name of the method followed by parenthesis ().
Syntax:

[return_type] [method_name] () {
     //body of method
     //statement;
     //code;
}

Example:

public class GeektoCode {
	void sum(int a, int b){
		int c = a+b;
		return c;      //return the value
	}
}

Interface

An interface is a group of the related method with empty bodies. Or you can also say that an interface in java is a blueprint of a class. It has static constants and abstract methods. It is a mechanism to directly achieve the concept of abstraction. So there can only be an abstraction method in Interface of Java without a method body. By using of interface we can achieve the functionality of multiple inheritances.
The declaration of an interface is performed with the ‘interface’ keyword. The methods which are declared within the interface must be accessible in another class by using the ‘implement’ keyword.
Syntax:

interface [interface_name]{
	//declare methods
	//The methods which are declare here that are abstract by default
}

OOPs Concept

OOP stands for Object-Oriented Programming. Object-Oriented Programming tries to map code instruction with real-world examples and make the code shorter and easier to understand. So Object-Oriented Programming aims to implement real-world entities like Inheritance, Polymorphism, Abstraction, Encapsulation, etc in programming. The main aim of Object-Oriented Programming is to bind together the data and methods.

Exception Handling

When a program is executed then an event occurs which is known as an exception. According to the dictionary meaning exception is an abnormal condition. So the handling of exceptions is one of the powerful mechanisms to manage and handle the runtime error. So due to exception handling the normal flow of the application can be maintained. Various types of exceptions occur during the program execution such as ClassNotFoundException, IOException, SQLException, ArrarIndexOutOfBoundsException, etc.

Multithreading

In java, the process of executing multiple threads simultaneously is known as Multithreading. Here threads use a shared memory area and the smallest unit of processing. Here multiprocessing and multithreading both are used to achieve the concept of multitasking.

By using multithreading we can perform many operations simultaneously. Threads are independent, so it doesn’t affect other threads during the runtime. Multithreading of Java is mostly used in various types of games, animation, etc.

So this is all about the structure of a java program. These are the various terms and concepts which are used in a java program. I hope this will help you. In the next upcoming post we will discuss another new topic of java i.e. Variables and Data types, so wait for this will meet in the next post. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *