;
Java Basics July 16 ,2025

Hello World & Java Syntax

In this module, we’ll dive into the very foundation of Java programming. Understanding how to write a basic "Hello World" program, learning the structure of a Java program, and exploring the syntax is essential for every beginner. This module also covers how to use comments effectively for writing clean and understandable code.

1. Writing Your First Java Program: "Hello World"

The "Hello World" program is often the first step in learning any programming language. It serves as a basic example to show how syntax works and how the output is displayed.

Here’s the simplest Java program:

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

Let's break it down:

  • public class HelloWorld: This defines a class named HelloWorld. In Java, everything must be inside a class. The file name must match the class name (HelloWorld.java).
  • public static void main(String[] args): This is the main method. It’s the entry point of any Java application. When you run a Java program, the JVM looks for this method to begin execution.
  • System.out.println("Hello, World!");: This is the statement that prints text to the console. System.out is Java’s output stream, and println() is a method that prints the string and moves the cursor to the next line.

Steps to Run:

  1. Save the file as HelloWorld.java.
  2. Compile: javac HelloWorld.java
  3. Run: java HelloWorld
  4. Output: Hello, World!

2. Java Program Structure

Understanding the structure is crucial before diving into complex logic. A Java program is made up of classes and methods. Here's a generalized structure:

// Package Declaration (optional)
package mypackage;

// Import Statements (optional)
import java.util.*;

// Class Declaration
public class MyProgram {

    // Main Method
    public static void main(String[] args) {
        // Statements or Logic
        System.out.println("Learning Java Structure");
    }

    // Additional Methods (Optional)
    public void greet() {
        System.out.println("Hello from another method!");
    }
}

Key Components:

  1. Package Declaration: Optional. It organizes classes into namespaces.
  2. Import Statements: Used to include built-in or user-defined classes.
  3. Class Declaration: Java is class-based, and all code must be inside a class.
  4. Main Method: Acts as the entry point for the program.
  5. Statements and Expressions: These are executable lines like method calls, assignments, etc.
  6. Methods: Functions inside classes. Java encourages code modularity through methods.

3. Java Syntax Basics

Java has a clean, C/C++-inspired syntax that emphasizes structure, readability, and consistency. Understanding the basic syntax is crucial because it forms the foundation of all Java programming. Syntax includes the rules and structure required to write Java code correctly.

Java Syntax: A Beginner's Guide to Writing Your First Java Program | by  Mouad Oumous | Javarevisited | Medium

Below are the fundamental syntax elements every student must learn, along with the conditions and conventions associated with them:

A. Case Sensitivity

Java is case-sensitive. This means that identifiers must match exactly in terms of uppercase and lowercase letters. For example, System, system, and SYSTEM are all considered different. This rule applies to variable names, class names, method names, and all identifiers in Java. A small change in case can lead to compile-time errors, making it essential to be precise.

B. Class Names

In Java, class names should always begin with an uppercase letter. This helps distinguish classes from variables and methods. When the name consists of multiple words, the camel case convention is followed—each word starts with an uppercase letter. This makes the code more readable and adheres to standard Java naming conventions.

Examples:

  • Student
  • EmployeeDetails

C. Method Names

Method names in Java should start with a lowercase letter and follow camelCase formatting. This means if the name has multiple words, the first word is lowercase, and each subsequent word starts with an uppercase letter. This style is used consistently across the Java standard library and is expected in user-defined methods as well.

Examples:

  • calculateSum()
  • printStudentInfo()

D. File Name

In Java, the filename must match the public class name within the file. If your class is named MainApp, the file must be saved as MainApp.java. Java uses this strict rule so that it can locate and compile the class properly. Mismatching the filename and class name will result in a compilation error.

Example:

public class HelloWorld {
    // code
}

Must be saved as HelloWorld.java

E. Java Statements

A statement in Java is a complete unit of execution and must end with a semicolon (;). This syntax rule tells the compiler where one instruction ends and another begins. Omitting the semicolon will lead to a syntax error during compilation.

Correct Example:

int x = 10;

Incorrect Example (missing semicolon):

int x = 10  // error

F. Blocks

Blocks in Java are defined using curly braces {} and group multiple statements into a single unit. Blocks are used in classes, methods, conditionals, loops, and other control structures. Every opening { must be matched with a closing }. Proper use of blocks ensures code is logically grouped and helps with readability and scope control.

Example:

if (score > 50) {
    System.out.println("Pass");
}

G. Whitespace

Whitespace refers to spaces, tabs, and new lines used in the code. The Java compiler ignores extra whitespace, so it doesn’t affect program output. However, proper use of whitespace is critical for making code readable and maintainable. Most developers follow common indentation practices to make the structure of the program clear to human readers.

Bad Practice:

int x=10;
System.out.println(x);

Good Practice:

int x = 10;
System.out.println(x);

4. Comments in Java

Comments are non-executable lines that are used to explain the code. They improve readability and help others understand the logic.

Types of Comments:

  1. Single-line Comments

    • Starts with //
    • Example:
    // This is a single-line comment
    System.out.println("Hello");
    
  2. Multi-line Comments

    • Starts with /* and ends with */
    • Example:
    /* This is a multi-line comment
       explaining the next few lines of code */
    System.out.println("Java");
    
  3. Javadoc Comments

    • Used to generate documentation using the javadoc tool.
    • Starts with /** and ends with */
    • Example:
    /**
     * This method prints a greeting.
     */
    public void greet() {
        System.out.println("Hello");
    }
    

Why Comments Are Important:

  • Make your code self-explanatory.
  • Useful when working in teams.
  • Helps during debugging and revisiting old code.
  • Necessary for creating professional documentation.

Common Mistakes Beginners Make (and How to Avoid Them)

  1. Missing Semicolon
    • Each statement must end with ;
  2. Wrong File Name
    • File name must match the public class name.
  3. Incorrect Method Signature
    • The main method must be written exactly as: public static void main(String[] args)
  4. Case Sensitivity Confusion
    • Use correct case for class names and methods.
  5. Unbalanced Braces
    • Ensure each { has a corresponding }.

Best Practices for Beginners

  • Use descriptive class and method names.
  • Indent code properly.
  • Add comments wherever necessary.
  • Keep methods short and focused on one task.
  • Practice writing small programs to reinforce concepts.

Example: Complete Program with Comments

// This is a basic Java program to demonstrate syntax
public class StudentIntro {

    // Main method: Entry point of the program
    public static void main(String[] args) {
        // Print a message to the console
        System.out.println("Hello, my name is Alex and I am learning Java!");
    }

    /**
     * This method is a placeholder for future expansion
     */
    public void displayMessage() {
        System.out.println("This is a sample method");
    }
}

Conclusion

The "Hello World" program and the basic syntax of Java form the bedrock of Java programming. By understanding how Java programs are structured, how to write statements correctly, and how to use comments effectively, you’re setting yourself up for success in your programming journey.

Take time to type out programs manually, explore variations, and understand each line before moving ahead. Mastery of the basics ensures a smoother transition to more advanced topics like variables, data types, conditionals, loops, and object-oriented programming.

 

Next Blog-  Java Variables: Understanding Variables, Printing, Multiple Variables & Identifiers

Sanjiv
0

You must logged in to post comments.

Get In Touch

123 Street, New York, USA

+012 345 67890

techiefreak87@gmail.com

© Design & Developed by HW Infotech