C

Core Java tutorial for beginners

Clean • Professional

Java Syntax & Structure – Basics for Beginners

2 minute

Java Syntax & Structure – Java Fundamentals

Before diving into object-oriented programming or advanced features, it’s essential to understand Java’s basic syntax and program structure.

This foundation helps you read, write, and organize Java code properly.


What is Java Syntax?

Syntax means the rules and structure that define how Java code must be written so the compiler can understand it.

Java syntax is strict but readable, inspired by C and C++.

Every Java program follows a clear, organized structure consisting of classes, methods, and statements.


Basic Structure of a Java Program

Here’s a simple example:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Welcome to Java!");
    }
}

Breakdown:

PartDescription
public class HelloWorldDefines a class named HelloWorld. The file name must match this class name.
{ ... }Curly braces define the body of a class or method.
public static void main(String[] args)Entry point of every Java program — where execution starts.
System.out.println("Welcome to Java!");Prints text to the console. Every statement ends with a semicolon ;.

Key Syntax Rules

  1. Case Sensitivity:

    Java is case-sensitive.

    MyClass and myclass are different identifiers.

  2. Class Names:

    Always start with an uppercase letter.

    Example: Student, BankAccount.

  3. Method Names:

    Use camelCase convention.

    Example: calculateTotal(), getUserName().

  4. File Name Rule:

    File name must match the public class name.

    → Class HelloWorld → File HelloWorld.java

  5. Main Method Requirement:

    The main() method is mandatory to execute a Java program.

    public static void main(String[] args)
    
  6. Statements End with Semicolons:

    Every executable statement ends with ;

  7. Comments:

    Used for notes or documentation (ignored by compiler).

    // Single-line comment
    /* Multi-line
       comment */
    

Common Elements of Java Programs

ElementExampleDescription
Package Declarationpackage myapp;Organizes related classes
Import Statementimport java.util.*;Includes built-in or external classes
Class Declarationpublic class MyClass { }Core building block of Java
Main Methodpublic static void main(String[] args)Entry point
StatementsSystem.out.println("Hi");Instructions to execute
Comments// ExampleNon-executable documentation

Example: Program with Package & Import

package basics;

import java.util.Scanner;

public class InputExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = sc.nextLine();
        System.out.println("Hello, " + name + "!");
        sc.close();
    }
}

Here,

  • package basics; → Groups the class inside a package named “basics”
  • import java.util.Scanner; → Imports the Scanner class from Java’s library
  • Scanner → Used to take input from the user

Whitespace & Indentation

  • Java ignores extra spaces, tabs, and newlines.
  • But indentation makes your code more readable and professional.

Example (Good Practice ):

public class Greet {
    public static void main(String[] args) {
        System.out.println("Hello!");
    }
}

Example (Bad Practice ):

public class Greet{public static void main(String[] args){System.out.println("Hello!");}}

Summary

ConceptKey Point
Case-sensitiveKeywords and identifiers must match case
Class structureOne public class per file
Main methodExecution starts from main()
StatementsMust end with ;
CommentsUsed for notes, ignored by compiler
IndentationNot mandatory but improves readability

 

Article 0 of 0