×

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully comprehend the fundamental structure of a Java program before delving further.

The structure of the Java program contains the following sections:

  1. Documentation Section
  2. Package Declaration
  3. Import Statements
  4. Interface Section
  5. Class Definition
  6. Class Variables and Variables
  7. Main Method Class
  8. Methods and Behaviours

Documentation Section

For a Java programme, the documentation part is a crucial but optional section. It contains essential details about a Java programme. The name of the author, creation date, version, programme name, firm name, and description of the programme are all included in the data. It makes the software easier to read. Whatever we put in the documentation section is ignored by the Java compiler when the programme is running. We employ comments to format the statements in the documentation section. Single-line, multi-line, and documentation comments are all acceptable types of comments.

  • Single-line Comment: It starts with a forward slash (//).

    For example: //this is a java single line comment
  • Multi-line Comment: It starts with /* and ends with */.

    For example: /* this is a multi-line comment in java
  • Documentation Comment: It starts with (/**) and ends with */.

    For example: /**this is documentation section in java */

Package section

The declaration of the package is optional, and follows the documentation section immediately. We identify the package name where the class is located in this section. Keep in mind that a Java programme can only have one package statement. Before the definition of any classes or interfaces, it must be declared. It is required because Java classes can be put in several packages and directories depending on the module, they are used in. The package has a single-parent guide that it belongs to for all of these classes. To declare the package name, we utilize the word package. For instance:

package Tutorial; //tutorial is the declared package
package example. Tutorial; // Tutorial is the main package; an example is a subpackage.

Import Statements

The numerous predefined classes and interfaces are included in the package. Any class from a specific package must be imported before it can be used. The class stored in the other package is represented by the import statement. To import the class, we utilize the import keyword. It appears both after the package statement and before the class definition. The import statement can be used to import either a single class or all the classes in a given package. We can utilize numerous import statements in a Java program. For instance:

import java.util.* // it will import all the classes and methods from util package
import java.util.scanner // it will only import the scanner class
import java.lang.*
import java .awt.* 

Interface section

This part is optional. If necessary, we can build an interface in this part. To create an interface, we utilize the interface keyword. A class and an interface differ slightly from one another. It just has declarations for methods and constants. It cannot be instantiated, which is another distinction. The implements keyword lets us use interface in classes. By utilizing the extends keyword, an interface can also be utilized with other interfaces. For instance:

interface animal
{
  void shout ();
void hunt ();
void eat ();
void sleep ();
}

Class Definition

The class is defined in this section. It plays a crucial role in a Java software. We are unable to write any Java programs without the class. One or more class definitions may be contained in a Java program. We use the class keyword to define the class. The class serves as a Java program's template. It includes details on user-defined procedures, variables, and constants. The main () method is present in at least one class in every Java program. For instance:

class Tutorial // class declaration
{
            // methods declaration
// variable declaration
  // logic of the program 
// executable statements
}

Class variables and constants

We define variables and constants in this part that will be utilized later in the program. The variables and constants are defined immediately following the class definition in a Java application. The values of the parameters are stored in the variables and constants. It is employed when the software is being run. By utilizing modifiers, we can additionally determine and specify the range of variables. It establishes the variables' life. For instance:

class Tutorial // class declaration
{
            int size ; // variable declaration 
String name ;
double average;
 
}

main() Method

We define the main () method in this section. Every Java application requires it. Because all Java applications start out running in the main () function, in other words, it acts as the beginning of the class, obviously in the classroom. In the main method, we create objects and invoke the methods. We invoke the main () method without creating an object. The main () function can be called without the construction of an object since static methods can be invoked without creating objects. To define the main () method, we use the following syntax:

public class Tutorial 
{
public static void main (String a [])
{
// Variable declaration
// object creation
// method declaration
// method calls
}
} 

Methods and Behavior

Using the methods, we define the program's functionality in this part. The set of instructions we want to follow is included in the methods. Runtime execution of these instructions completes the requested task. A method is a part, group, or body of code that is used to carry out a certain operation or action. Code can become more reused thanks to it. Once developed, a technique is applied repeatedly. You are not needed to write the same code repeatedly. Additionally, simply adding or removing a block of code, it provides straightforward code modification and readability. The method is executed when it is called or invoked. For instance:

public class Tutorial 
{
public static void main (String a [])
{
void display ()
{
system.out.print(“Welcome to tutorial and example”);
}
// statements
}
} 

Let’s try to write a program by following all the above documentation steps:

StringCount.java

/* java program Documentation
A string is provided to you; your task is to determine the frequency of each character in the string and output that information. You can solve this problem by utilizing a hashing algorithm or a hash map. When generating a hash map, you must give a character as the key record and an integer as the value record. The characters of the given string must now be stored in the key record, according to logic. You must keep track of how many characters are in the string in each key-value record. If a character appears again, the hash map's frequency count for that character needs to be increased. Finally, you need to traverse the hash map and print the character along with its count values */
// import the required packages


import java.io.*;
import java.util.*;
import java.lang.*;
public class StringCount
// class declaration
{
public static void main (String [] args) 
  // main method declaration
{
Scanner sc = new Scanner (System.in);
// scanner class for input at runtime
                      System.out.println(“Please enter the String:”);
String repString = sc.next ();
// string input
FrequencyString StringFreq=new FrequencyString();
// object creation and calling
     StringFreq.Frequency (repString);
}
}
class FrequencyString
{
    void Frequency (String s)
// not static method of FrequencyString class out of main class
    {
        HashMap<Character,Integer> Freq = new HashMap <> ();
        // Hash map creation for storing characters and integers for easy traversal
        for (int i = 0; i < s.length (); i++)
        {
            if (Freq.containsKey (s.charAt(i)))
            {
                Freq.put (s.charAt (i), Freq.get (s.charAt (i))+1);
// increasing count if the character is already present in the string
            }
            else
            {
                Freq.put (s.charAt (i),1);
     // Storing each character of string in hashmap if it is not present in the  
       // hashmap
                
            }
        }
        
        for (int i = 0; i < s.length (); i++)
        {
            if (Freq.get (s.charAt (i)) != 0)
            {
                System.out.println(s.charAt (i)+" -> "+Freq.get (s.charAt (i)));
       // printing the character frequency in the string along with the 
                // character
                Freq.put (s.charAt (i),0);
            }
        }
    }
}

Output:

Structure of Java Program

Related Topics

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Diamond problem in Java

The Diamond Problem in Java is connected to multiple inheritances. It is also referred to as the "deadly diamond dilemma" or even the "deadly diamond of death”. The solution for...

5 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 minutes read.

Libraries in Java

The Java Class Library (JCL) specifically is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at any run time, which is fairly significant because...

6 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Time Class Operation in Java

The Java SQL package includes the time class. This class is merely a lightweight wrapper for java. util. THANKS TO THE recognize BC API can recognize this as a SQL...

3 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

How to run Java Program?

How to run Java Program In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad. In order to run a Java program, we...

2 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.