Java Tutorial



Java Tutorial

Structure of Java Program and Java Syntax


The structure of Java programs and Java syntax offer a sturdy groundwork for the coding endeavors of developers. The structure involves organizing the program, where classes, methods, and statements play crucial roles. Similarly, Java syntax establishes the rules and conventions for writing code in the language.

Having a firm grasp of these aspects enhances code organization and readability for programmers and promotes collaboration among developers.  With a solid understanding of Java's structure and syntax, you can efficiently debug and troubleshoot during development. 


This tutorial covers the structure of Java programs and Java syntax. It will help you grasp the crucial concepts to ensure your programs are structured, comprehensive, and error-free. Read on and upgrade your programming skills to a whole new level. 

What is Java Syntax?

Java syntax entails a set of rules and conventions that dictate how code is written in the Java programming language. It encompasses guidelines for creating statements, defining variables, declaring methods, and organizing classes. The syntax serves as a framework for articulating logic and functionality in a structured and understandable way. 

Class Section

In Java, the term "class section" refers to a specific segment or division within a class that encompasses related elements, including variables and methods. Its purpose is to arrange and cluster functionalities within the class, making it easier to maintain code.

Syntax: public class ClassName {

This line declares a class named ClassName. In Java, a class is defined using the class keyword followed by the class name. The class body is enclosed within curly braces {}.

main() Method

The "main()” method in Java is the initial entry point for executing any Java program. It serves as the starting point at which the program begins its execution. This method is declared with the public access modifier, static keyword, and a return type of void (meaning it doesn't return any value). It takes a single parameter of type String[] named args, which allows command-line arguments to be passed to the program. The code to be executed is written within the method body.

Syntax: public static void main(String[] args) {


In Java, the System.out.printf() method fulfills the purpose of formatting and presenting the output on the console. This method allows you to display data in a desired format, incorporating placeholders for precise value placement. This feature offers the flexibility to customize the presentation of information during program execution.

Syntax: System.out.printf(format, args)

The System.out.printf() method prints formatted output to the console. It takes a format string as the first argument, specifying the output format. The format string may include format specifiers, such as %d for integers, %f for floating-point numbers, %s for strings, etc. The actual values to be printed are provided as additional arguments (args) according to the format string.

Parameters of System.out.printf() method

The "System.out.printf()" method in Java accepts multiple parameters. The first parameter is a format string specifying the output's desired formatting. It may include placeholders to indicate where the values from the subsequent parameters should be inserted. The additional parameters are the values that will be inserted into the placeholders within the format string. The number and type of these additional parameters depend on the format specified in the format string.

In the above example, the System.out.printf() statement includes three variables as additional arguments, which substitute their corresponding placeholders in the format string.

Here is an example where we use Class, main() Method, and System.out.printf() to create a Java program:

public class main {
    public static void main(String[] args) {
        int age = 25;
        double salary = 5000.50;
        String name = "John Doe";
        System.out.printf("Name: %s, Age: %d, Salary: %.2f", name, age, salary);

Basic terminologies in Java

Java terminologies lay the groundwork for writing and understanding Java programs.

  • Class: It serves as a blueprint or template for generating objects and defining their attributes and behaviors.

  • Object: An object is an instance of a class. It represents a specific entity with its own set of attributes and behaviors.

  • Method: A method is a block of code that performs a specific task. It is defined within a class and can be called to execute the code it contains.

  • Variable: A variable is a named storage location that holds a value. It can store different data types, such as numbers, text, or objects.

  • Statement: A statement denotes a single line of code executing an action or yielding a result sequentially within a method.

  • Loop: A loop constitutes a control structure permitting the repeated execution of a code block until a specific condition is satisfied.

  • Condition: It constitutes an expression that evaluates to true or false, employed in decision-making structures like loops and if statements to regulate program flow.

Case Sensitive Language

The structure of Java programs and Java syntax is case-sensitive, meaning that it distinguishes between uppercase and lowercase letters in its code. This sensitivity applies to identifiers such as variable names, method names, class names, and keywords.

For example, "myVariable" and "myvariable" would be treated as distinct identifiers in Java. It is crucial to be mindful of the correct letter casing when working with identifiers to ensure the proper functioning of the program.

Java Identifiers 

Java employs identifiers to designate variables, methods, classes, packages, and other program elements. These identifiers serve as markers or labels for said elements and have a pivotal impact on the legibility and comprehensibility of the code.

Valid identifiers

In Java, valid identifiers comply with specific guidelines. They must commence with a letter, an underscore (_), or a dollar sign ($). The following characters may encompass letters, digits, underscores, or dollar signs. Additionally, Java reserves specific keywords that cannot be employed as identifiers.

Invalid identifiers

Invalid identifiers encompass names that contravene the regulations governing Java identifiers.

Declaring Variables 

Variables store and manipulate data within a program. Declaring a variable in the structure of Java programs and Java syntax entails specifying the variable's type and assigning it a name. The declaration typically comprises the variable's data type, such as int, double, or String, followed by the chosen identifier for the variable.

Furthermore, variable names cannot be Java keywords and should be descriptive, reflecting the purpose or meaning of the stored data.


Java Modifiers

Modifiers are keywords in Java that can be applied to classes, methods, variables, and other program elements to specify their visibility, accessibility, and behavior. 

There are mainly two types of modifiers, access modifiers, and non-access modifiers.

Access Modifiers

Here are some commonly used access modifiers:

  • public: The public modifier allows the class, method, or variable to be accessed from anywhere.

Example Syntax:

public class MyClass {
    public int myVariable;
    public void myMethod() {
  • private: The private modifier restricts the visibility of the class, method, or variable to within the same class.

Example Syntax:

public class MyClass {
    private int myVariable;
    private void myMethod() {
  • protected: The protected modifier allows access to the class, method, or variable within the same package or subclasses.

Example Syntax:

public class MyClass {
    protected int myVariable;
    protected void myMethod() {
  • Default (no modifier): If no access modifier is specified, the class, method, or variable has package-level visibility (accessible within the same package).

Example Syntax:

class MyClass {
    int myVariable;
    void myMethod() {

Non-Access Modifiers

  • static: The static modifier is used to create class-level members (variables or methods) that belong to the class rather than specific instances of the class.

Example Syntax:

public class MyClass {
    public static int myVariable;
    public static void myMethod() {
  • final: The final modifier declares a constant value or makes a class, method, or variable unmodifiable.

Example Syntax:

public final class MyClass {
    public static final int MY_CONSTANT = 10;
    public final void myMethod() {
        // Method body
  • abstract: The abstract modifier defines abstract classes or methods which cannot be instantiated and require subclasses to provide implementations.

Example Syntax:

public abstract class MyAbstractClass {
    public abstract void myAbstractMethod();
  • synchronized: The synchronized modifier provides thread safety by ensuring that only one thread can access a synchronized method or block at a time.

Example Syntax:

public class MyClass {
    public synchronized void myMethod() {
        // Synchronized method body

List of Keywords in Java 

Here is a list of keywords in Java:

Documentation Section

Documentation is crucial in providing information about classes, interfaces, methods, and other program elements. It helps developers understand the code's purpose, behavior, and usage.

Documentation is typically written using comments and special tags that documentation tools like Javadoc can process to generate HTML documentation

Package Declaration

In Java, a package declaration is used to specify the package to which a particular Java class belongs. It is typically the first line of code in a Java source file and helps organize classes into logical groups.

Example Syntax: package;

Here, is an example of a package name. It follows the convention of using a reverse domain name as the package identifier. The package name is hierarchical, with components separated by dots (.).

Import Statements

In Java, import statements bring classes, interfaces, and other program elements from other packages into the current source file. It allows you to reference and use those elements without fully qualifying their names with the package name every time.

The import statement typically appears at the beginning of a Java source file, after the package declaration (if present), and before the class declaration. There are different ways to import elements from other packages:


  • import com.example.myapp.MyClass; (For importing a class named MyClass from the com.example.myapp package)

  • import com.example.myapp.utils.*; (For importing all the classes and interfaces from the utils package)

  • import static java.lang.Math.*; (For importing all the static members (fields and methods) from the Math class in java.lang package.

Interface Section 

Interfaces are used to achieve abstraction and define common behavior that multiple classes can share. In Java, an interface is a reference type that defines a contract of methods to which a class implementing the interface must adhere. It specifies a set of methods (and potential constants) that any implementing class must provide.

Example syntax:

public interface Drawable {
    void draw();
    double calculateArea();

In this example, we define an interface called Drawable. It declares two methods: draw() and calculateArea(). Any class that implements this interface must provide an implementation for these methods.

Program File Name

In Java, the program file name must match the public class name defined in the file. The file should have a .java extension.

For example, if you have a public class named MyProgram, the file should be named


In summary, having a solid understanding of the structure and syntax of Java is essential for effective programming. The way Java programs are organized, and the rules for writing code play a crucial role in creating readable and error-free software. By mastering these aspects, programmers can develop well-structured and maintainable code. 

To embrace the fundamentals of the structure of Java programs and Java syntax, consider signing up for a top-tier professional course offered by upGrad.


1. Why is it essential to have a solid grasp of the structure of Java programs and Java syntax?

Understanding the structure of Java programs and syntax holds great importance as it provides a well-organized and logical flow to the code. A well-structured program promotes clarity, modularity, and reusability, simplifying development and collaboration with fellow programmers.

2. What are the benefits of adhering to Java syntax rules?

Complying with Java syntax rules is crucial for producing accurate and error-free code. Adhering to syntax rules minimizes syntax errors, ensures compatibility with Java's runtime environment, and facilitates efficient code maintenance.

3. How does a strong understanding of Java syntax help in the debugging process?

A solid understanding of Java syntax greatly assists in debugging and troubleshooting code. Developers can swiftly locate and rectify bugs by accurately interpreting the program's structure and syntax, resulting in shorter development cycles and improved software quality.

Leave a Reply

Your email address will not be published. Required fields are marked *