BASIC STRUCTURE OF A JAVA PROGRAM
In this post you will learn the basic structure of a java program and what each line in that structure means
Below is the photo of basic structure of a java program
1.Documentation Section :
It contains comments that increase the program's readability. A comment is a non-executable statement that aids in the reading and understanding of a program, particularly as it becomes more sophisticated. It's just a message that only the programmer sees, and the compiler ignores it. A good program should include comments that indicate the program's objective, author name, and creation date and time. Comments may appear anywhere in the program, and this area is optional.
Now lets see the type of comments we can use in java programming,three different sorts of comments are supported by the Java programming language.
i.Single Line Comment : It begins with a double slash symbol (//) and ends with the current line's end. Everything from / to the end of the line is ignored by the compiler. Example : // This is a single line comment
ii.Multi Line Comment : It starts with a /* and ends with */. We write between these two symbols. Example : /* This is a multi line comment */
iii.Documentation Comment : It starts with the delimiter (/**) and ends with */. Example: /**This is an Documentation type comment*/
2.Package Declaration Section :
The declaration of a package is optional. It is immediately following the documentation section. The package name in which the class is stored is declared in this section. In a Java application, there can only be one package statement. It must be defined prior to the declaration of any classes or interfaces. It's required because, depending on the module, a Java class can be stored in various packages and folders. The package for all of these classes has a single parent directory. To declare the package name, we utilise the term package.
3.Import Statements Section :
Many predefined classes and interfaces are included in the package. If we wish to utilise a class from a specific package, we must first import it. The class stored in the other package is represented by the import line. To import the class, we utilise the import keyword. It comes after the package statement and before the class declaration. We may use the import statement in two ways: to import a single class or to import all of the classes in a given package. Multiple import statements can be used in a Java program.
4.Interface Section :
This is a part that can be skipped. If necessary, we can construct an interface in this part. To create an interface, we utilise the interface keyword. An interface differs from a class in a few ways. It only has constants and method declarations in it. Another distinction is that it is not instantiable. The implements keyword can be used to use interface in classes. The extends keyword allows an interface to be used with other interfaces.
5.Class Definition Section :
The class is defined in this section. It is an essential component of a Java program. We can't write a Java program without the class. A Java program can have many class definitions. To define the class, we utilise the class keyword. The class is a Java program's blueprint. It includes data on user-defined methods, variables, and constants. The main() method is found in at least one class in every Java program.
6.main method class Section :
The main( ) method is defined in this section. It's a must for all Java programmes. Because the main( ) function is where all Java applications begin to run. In other words, it is the class's starting point. It's have to be within the class. We construct objects and invoke methods in the main method.
Example Program:
Given below is an example of a complete Java program for the addition and multiplication of two numbers with the use of the above sections.
The above sections of the programme are discussed below :
import the java.util.Scanner package.
The import statement refers to the util package's Scanner class. The user input is collected using the Scanner class.
public class calculator
The preceding sentence is for the definition of the class "calculator" with the "public" access modifier.
public void add( )
It's a method for adding two numbers that has the "public" access modifier.
public void multiply( )
It's a method for multiplying two numbers with the "public" access modifier.
main public static void (String []args)
When the compiler runs our program, it always starts from the main( ) method ( ). When invoking the main( ) method without generating its object, the keyword static is used. The method is made public so that another package can call the main () method. The keyword "void" denotes that no value is returned. If your program is performed through the console, the args[] array is supplied as a String data type and is used to take input parameters.
System.out.println( )
The line is used to print text, with System being a predefined class and out being a printWriter object. The println() method is used to print the text as output on the screen with a new line.
Comments
Post a Comment