***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***

posts

    Structure of Java Program

    A Java program involves the following sections:

    Documentation Section

    The documentation section is an important section but optional for a Java program. It includes basic information about a Java program. The information includes the author's name, date of creation, version, program name, company name, and description of the program.

    It improves the readability of the program. Whatever the documentation section contains, the Java compiler ignores the statements during the execution of the program.

    To write the statements in the documentation section, we use comments. The comments may be single-line, multi-line, and documentation comments.

    • Single-line Comment: It starts with a pair of forwarding slash (//). For example:

    //First Java Program  

    • Multi-line Comment: It starts with a /* and ends with */. We write between these two symbols. For example:

    /*It is an example of 

    multiline comment*/  

    • Documentation Comment: It starts with the delimiter (/**) and ends with */. For example:

    /**It is an example of documentation comment*/  

    Package Declaration

    The package declaration is optional. It is placed just after the documentation section. The package name in which the class is placed is declared in this section. There can be only one package statement in a Java program. It must be defined before any class and interface declaration. The keyword “package” is used to declare the package name.

    For example:

    package Mypackage;

    Import Statements

    The package contains the many predefined classes and interfaces. If any class of a particular package is to be used, that class has to be imported.

    The import statement represents the class stored in the other package. The “import” keyword is used to import the class. It is written before the class declaration and after the package statement. The import statement is used in two ways, either import a specific class or import all classes of a particular package. In a Java program, we can use multiple import statements.

    For example:

    1. import java.util.Scanner; //it imports the Scanner class only  
    2. import java.util.*; //it imports all the class of the java.util package  

    Interface Section

    It is an optional section. An interface can be created in this section if required. The “interface” keyword is used to create an interface. An interface is a slightly different from the class.

    It contains only constants and method declarations. Another difference is that it cannot be instantiated. An interface can be used in classes by using the “implements” keyword. An interface can also be used with other interfaces by using the “extends” keyword.

    Class Definition

    It is vital part of a Java program. A Java program may conation more than one class definition. The “class” keyword is used to define the class. The class is a blueprint of a Java program. It contains information about user-defined methods, variables, and constants. Every Java program has at least one class that contains the main() method.

    class Student //class definition  

    {  

    }  

    Class Variables and Constants

    In this section, variables and constants that are to be used later in the program are defined. In a Java program, the variables and constants are defined just after the class definition. The variables and constants store values of the parameters. It is used during the execution of the program.

    class Student //class definition  

    {  

    String sname;  //variable  

    int id;   

    double percentage;   

    }  

    Main Method Class

    In this section, the main() method is defined. It is essential for all Java programs. Because the execution of all Java programs starts from the main() method.

    In other words, it is an entry point of the class. It must be inside the class. Inside the main method, we create objects and call the methods.

    public class Student //class definition  

    {  

    public static void main(String args[])  

    {  

    //statements  

    }  

    }  

    Methods and behavior

    In this section, the functionality of the program is defined by using the methods. The methods are the set of instructions that we want to perform. These instructions execute at runtime and perform the specified task.

    public class Demo //class definition  

    {  

    public static void main(String args[])  

    {  

    void display()  //method

    {  

    System.out.println("Welcome to javaclass");  

    }  

    //statements  

    }  

    }  

    No comments:

    Post a Comment