0% found this document useful (0 votes)
1 views

Structure of Java Program

The document outlines the basic structure of a Java program, including essential elements such as the documentation section, package declaration, import statements, class definition, and the main method. It explains the purpose of each section and provides examples of comments, class definitions, and methods. By understanding these components, readers will be able to develop a simple Java program, such as a Hello World application.

Uploaded by

Infinite Elixir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Structure of Java Program

The document outlines the basic structure of a Java program, including essential elements such as the documentation section, package declaration, import statements, class definition, and the main method. It explains the purpose of each section and provides examples of comments, class definitions, and methods. By understanding these components, readers will be able to develop a simple Java program, such as a Hello World application.

Uploaded by

Infinite Elixir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Structure of Java Program

Java is an object-oriented programming, platform-independent, and secure


programming language that makes it popular. Using the Java programming language,
we can develop a wide variety of applications. So, before diving in depth, it is necessary
to understand the basic structure of Java program in detail. In this section, we have
discussed the basic structure of a Java program. At the end of this section, you will able
to develop the Hello world Java program, easily.

Let's see which elements are included in the structure of a Java program. A typical
structure of a Java program contains the following elements:

○ Documentation Section

○ Package Declaration

○ Import Statements
○ Interface Section

○ Class Definition

○ Class Variables and Variables

○ Main Method Class

○ Methods and Behaviors

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 we write in the
documentation section, 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:

1. //First Java Program

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

1. /*It is an example of
2. multiline comment*/

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

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

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. Note that there
can be only one package statement in a Java program. It must be defined before any
class and interface declaration. It is necessary because a Java class can be placed in
different packages and directories based on the module they are used. For all these
classes package belongs to a single parent directory. We use the keyword package to
declare the package name. For example:

1. package javatpoint; //where javatpoint is the package name


2. package com.javatpoint; //where com is the root directory and javatpoint is the
subdirectory

Import Statements
The package contains the many predefined classes and interfaces. 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. We
use the import statement 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. 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. Another difference is that it
cannot be instantiated. We can use interface in classes by using the implements
keyword. An interface can also be used with other interfaces by using the extends
keyword. For example:

1. interface car
2. {
3. void start();
4. void stop();
5. }

Class Interface

The keyword used to create a The keyword used to create an interface

class is “class” is “interface”

A class can be instantiated i.e., An Interface cannot be instantiated i.e.

objects of a class can be created. objects cannot be created.

Classes do not support multiple The interface supports multiple

inheritance. inheritance.
It can be inherited from another
It cannot inherit a class.
class.

It can be inherited by a class by using


It can be inherited by another
the keyword ‘implements’ and it can be
class using the keyword
inherited by an interface using the
‘extends’.
keyword ‘extends’.

It can contain constructors. It cannot contain constructors.

It cannot contain abstract


It contains abstract methods only.
methods.

All variables and methods in an


Variables and methods in a class
interface are declared as public.
can be declared using any access
specifier(public, private, default,

protected).

Variables in a class can be static,


All variables are static and final.
final, or neither.

Class Definition
In this section, we define the class. It is vital part of a Java program. Without the class,
we cannot create any Java program. A Java program may contain more than one class
definition. We use the class keyword 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.
For example:

1. class Student //class definition


2. {
3. }

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. The variables and constants store values of the parameters. It is used during
the execution of the program. We can also decide and define the scope of variables by
using the modifiers. It defines the life of the variables. For example:

1. class Student //class definition


2. {
3. String sname; //variable
4. int id;
5. double percentage;
6. }

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. We use the following statement to
define the main() method:

1. public static void main(String args[])


2. {
3. }

For example:

1. public class Student //class definition


2. {
3. public static void main(String args[])
4. {
5. //statements
6. }
7. }

You can read more about the Java main() method here.

Methods and behavior


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. For example:

1. public class Demo //class definition


2. {
3. public static void main(String args[])
4. {
5. void display()
6. {
7. System.out.println("Welcome to javatpoint");
8. }
9. //statements
10. }
11. }

When we follow and use the above elements in a Java program, the program looks like
the following.

CheckPalindromeNumber.java

1. /*Program name: Palindrome*/


2. //Author's name: Mathew
3. /*Palindrome is number or string that will remains the same
4. When we write that in reverse order. Some example of
5. palindrome is 393, 010, madam, etc.*/
6. //imports the Scanner class of the java.util package
7. import java.util.Scanner;
8. //class definition
9. public class CheckPalindromeNumber
10. {
11. //main method
12. public static void main(String args[])
13. {
14. //variables to be used in program
15. int r, s=0, temp;
16. int x; //It is the number variable to be checked for palindrome
17. Scanner sc=new Scanner(System.in);
18. System.out.println("Enter the number to check: ");
19. //reading a number from the user
20. x=sc.nextInt();
21. //logic to check if the number id palindrome or not
22. temp=x;
23. while(x>0)
24. {
25. r=x%10; //finds remainder
26. s=(s*10)+r;
27. x=x/10;
28. }
29. if(temp==s)
30. System.out.println("The given number is palindrome.");
31. else
32. System.out.println("The given number is not palindrome.");
33. }
34. }

Output:

You might also like