0% found this document useful (0 votes)
36 views5 pages

The Basic Structure of A Java Program

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
36 views5 pages

The Basic Structure of A Java Program

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Structure of a Java program

1. Documentation Section
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. Using comments we can write in the documentation section. The comments
may be single-line, multi-line, and documentation comments.
Example:
Single line comment: //
Multi-line Comment:/* …..*/
Documentation Comment: /** …….*/
2. Package Declaration
The package declaration is optional. It is placed just after the documentation section. In
this section, we declare the package name in which the class is placed. There can be only
one package statement in a Java program. It must be defined before any class and
interface declaration.We use the keyword package to declare the package name.
Ex.package packagename;
3. Import Statements
If we want to use any class of a particular package, we need to import that class. The
import statement represents the class stored in the other package. We use the import
keyword to import the class. It is written before the class declaration and after the
package statement.
Ex. import java.util.*;
Import java.util.scanner;
4. Interface Section
It is an optional section. We can create an interface in this section if required. We use the
interface keyword to create an interface. An interface is a slightly different from the class.
It contains only constants and method declarations.
Ex. interface runnable{
void run();
}
5. Class Definition
In this section, we define the class.Without the class, we cannot create any Java program.
We use the class keyword to define the class. It contains information about user-defined
methods, variables, and constants. Every Java program has at least one class that contains
the main() method.
6. Class Variables and Constants
In this section, we define variables and constants that are to be used later in the program.
In a Java program, the variables and constants are defined just after the class definition.
Ex.
Class Demo{
Int a;
void show()
{
}
}
7. Main Method Class
In this section, we define the main() method. 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.
Methods
In this section, we define the functionality of the program 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.
Ex.
public static void main(String args[])
{
int a=10;
void show(){
System.out.println(“value of a is:”+a);
}
}

Phases of java program Development


There are five phases needed to run the Java program.
Phase-1: Edit
When we write a Java program on any editor & save it, then it is stored in a disk with an
extension of .java. This .java file is platform-independent

Phase-2: Compile
The compiler’s work is to convert the high-level language to bytecodes then store it with an
extension of .class. To compile we use the “javac” command.The compiler checks for syntax
errors, and if everything is correct then it starts translating the .java file else it gives a
compile-time error. After compilation of the .java file, the .java file is translated to .class file.

Phase-3: Load
The class loader reads the code, compiles it, and puts those bytecodes from the disk to primary
memory. From the class loader, the codes are stored in primary memory. When we execute the
“java” command to run the Java application then the .class file (which contains bytecode) is
loaded into the RAM, and this task is done by the class loader.

Phase-4: Verify
This step confirms that the bytecodes are valid and make sure that they do not risk the Java
security restrictions.

Phase-5: Execute
In this step, the JVM (Java Virtual Machine) reads the byte and translates it to the language that
the machine can understand that is machine language then executes the program and stores it in
values in primary memory
If we use only one & or | then it's known as “ bitwise AND” and bitwise OR operators and if we
use double && or || then it's known as logical or short-circuit AND and OR operators. From the
name, you can guess bitwise operates at the bit level and perform AND logical operation to each
bit, while logical operators operate on boolean variables only.

You might also like