Structure of Java Program - Javatpoint
Structure of Java Program - Javatpoint
Ad
Special Bhai Bhabi Rakhi
Winni : Celebrte Relation
https://www.javatpoint.com/structure-of-java-program 1/10
9/1/2021 Structure of Java Program - Javatpoint
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
Documentation Section
https://www.javatpoint.com/structure-of-java-program 2/10
9/1/2021 Structure of Java Program - Javatpoint
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:
//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. 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:
https://www.javatpoint.com/structure-of-java-program 3/10
9/1/2021 Structure of Java Program - Javatpoint
package javatpoint; //where javatpoint is the package name
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:
import java.util.Scanner; //it imports the Scanner class only
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:
interface car
{
void start();
void stop();
}
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 conation 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:
class Student //class definition
{
https://www.javatpoint.com/structure-of-java-program 4/10
9/1/2021 Structure of Java Program - Javatpoint
}
class Student //class definition
{
String sname; //variable
int id;
double percentage;
}
public static void main(String args[])
{
}
For example:
public class Student //class definition
{
public static void main(String args[])
{
//statements
}
}
You can read more about the Java main() method here.
https://www.javatpoint.com/structure-of-java-program 5/10
9/1/2021 Structure of Java Program - Javatpoint
public class Demo //class definition
{
public static void main(String args[])
{
void display()
{
System.out.println("Welcome to javatpoint");
}
//statements
}
}
When we follow and use the above elements in a Java program, the program looks like the following.
CheckPalindromeNumber.java
/*Program name: Palindrome*/
//Author's name: Mathew
/*Palindrome is number or string that will remains the same
When we write that in reverse order. Some example of
palindrome is 393, 010, madam, etc.*/
//imports the Scanner class of the java.util package
import java.util.Scanner;
//class definition
https://www.javatpoint.com/structure-of-java-program 6/10
9/1/2021 Structure of Java Program - Javatpoint
public class CheckPalindromeNumber
{
//main method
public static void main(String args[])
{
//variables to be used in program
int r, s=0, temp;
int x; //It is the number variable to be checked for palindrome
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to check: ");
//reading a number from the user
x=sc.nextInt();
//logic to check if the number id palindrome or not
temp=x;
while(x>0)
{
r=x%10; //finds remainder
s=(s*10)+r;
x=x/10;
}
if(temp==s)
System.out.println("The given number is palindrome.");
else
System.out.println("The given number is not palindrome.");
}
}
Output:
← Prev
Next →
For Videos Join Our Youtube Channel: Join Now
https://www.javatpoint.com/structure-of-java-program 7/10
9/1/2021 Structure of Java Program - Javatpoint
Feedback
in top companies
Preparation
Company Questions
Trending Technologies
B.Tech / MCA
C++ tutorial
C++
https://www.javatpoint.com/structure-of-java-program 10/10