Skip to main content
Java Basics
CHAPTER 03 Beginner

Java Syntax and First Program

Updated: May 17, 2026
5 min read

# CHAPTER 3

Java Syntax and First Program

1. Introduction

Every programming language has grammar rules — its syntax. Just as English requires subjects, verbs, and periods, Java requires classes, methods, and semicolons. In this chapter, we'll dissect a Java program line by line so you understand *every single keyword* and build a working calculator.

2. Learning Objectives

  • Understand the anatomy of a Java program.
  • Explain the purpose of public, static, void, and main.
  • Use System.out.println() and System.out.print().
  • Write single-line and multi-line comments.
  • Build a basic calculator program.

3. Anatomy of a Java Program

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

Let's break down every single keyword:

ComponentMeaning
publicAccess modifier — this class/method is accessible from anywhere
classKeyword to declare a class (the blueprint)
HelloWorldThe name of the class (must match the filename)
staticThe method belongs to the class itself, not to an instance/object
voidThe method does not return any value
mainThe entry point — JVM looks for this method to start execution
String[] argsAn array of command-line arguments
System.out.println()Prints text to the console and adds a new line

4. The main() Method — The Entry Point

Every Java application must have exactly one main method. It is the starting gate of your program.
java
123
public static void main(String[] args) {
    // Your code starts here
}

Real-World Analogy: Think of main() as the front door of a building. The JVM always enters through this door first.

5. Printing Output

System.out.println() — Prints text and moves to a new line.

java
12
System.out.println("Line 1");
System.out.println("Line 2");

Output:

12
Line 1
Line 2

System.out.print() — Prints text but stays on the same line.

java
12
System.out.print("Hello ");
System.out.print("World");

Output:

1
Hello World

System.out.printf() — Formatted output (like C's printf).

java
123
String name = "Alice";
int age = 25;
System.out.printf("Name: %s, Age: %d%n", name, age);

Output:

1
Name: Alice, Age: 25

6. Comments in Java

Comments are ignored by the compiler. They exist to explain your code to other developers (and your future self).
java
12345678910
// This is a single-line comment

/* This is a
   multi-line comment */

/**
 * This is a Javadoc comment.
 * Used to generate official API documentation.
 * @author John Doe
 */

7. Semicolons and Curly Braces

  • Every statement must end with a semicolon ;.
  • Blocks of code are wrapped in curly braces { }.
java
12345
public class Rules {
    public static void main(String[] args) {    // Opening brace
        System.out.println("Semicolons matter!"); // Semicolon
    }                                              // Closing brace
}

8. Escape Characters

Special characters inside strings:
EscapeOutput
\nNew line
\tTab space
\\Backslash
\"Double quote
java
123
System.out.println("Hello\tWorld");
System.out.println("She said \"Java is amazing!\"");
System.out.println("C:\\Users\\Documents\\file.txt");

Output:

123
Hello	World
She said "Java is amazing!"
C:\Users\Documents\file.txt

9. Java is Case-Sensitive

Java treats uppercase and lowercase letters as completely different:
  • Systemsystem
  • Stringstring
  • Mainmain

A single wrong capitalization will crash your program.

10. Naming Conventions

EntityConventionExample
ClassPascalCaseStudentReport
MethodcamelCasecalculateTotal()
VariablecamelCasefirstName
ConstantUPPERSNAKECASEMAX_SIZE
Packagelowercasecom.myapp.utils

11. Mini Project: Basic Calculator

java
1234567891011121314151617
public class BasicCalculator {
    public static void main(String[] args) {
        int a = 20;
        int b = 5;

        System.out.println("===== BASIC CALCULATOR =====");
        System.out.println("Number 1: " + a);
        System.out.println("Number 2: " + b);
        System.out.println("----------------------------");
        System.out.println("Addition:       " + (a + b));
        System.out.println("Subtraction:    " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division:       " + (a / b));
        System.out.println("Modulus:        " + (a % b));
        System.out.println("============================");
    }
}

Output:

12345678910
===== BASIC CALCULATOR =====
Number 1: 20
Number 2: 5
----------------------------
Addition:       25
Subtraction:    15
Multiplication: 100
Division:       4
Modulus:        0
============================

12. Common Mistakes

  • Missing semicolon: System.out.println("Hi") → Compilation error.
  • Wrong class name: File is Hello.java but class is HelloWorld → Error.
  • Using System.out.Println (capital P) → Java is case-sensitive, this fails.

13. Best Practices

  • Always start class names with an uppercase letter.
  • Use meaningful names: calculateSalary() not cs().
  • Comment your code, but don't over-comment obvious lines.

14. Exercises

  1. 1. Write a program that prints your name, age, and city on three separate lines.
  1. 2. Write a program using only System.out.print() (not println) to print "Java is fun!" on one line using three separate print statements.
  1. 3. Create a program that demonstrates all four escape characters.

15. MCQ Quiz with Answers

Question 1

What is the entry point of every Java application?

Question 2

What does System.out.println() do?

Q3. Which is a valid single-line comment in Java? a) <!-- comment --> b) # comment c) // comment d) -- comment Answer: c) // comment
Question 4

Every Java statement must end with a:

Question 5

Java is:

Question 6

What naming convention do Java classes follow?

Question 7

What does \t produce in a string?

Question 8

Which keyword means the method doesn't return any value?

Question 9

What does String[] args represent in the main method?

Question 10

What happens if the filename doesn't match the public class name?

16. Interview Questions

  • Q: Explain every keyword in public static void main(String[] args).
  • Q: What is the difference between println(), print(), and printf()?
  • Q: Can a Java program have multiple classes? Can it have multiple main methods?
  • Q: What are Javadoc comments and how are they used?

17. FAQs

Q: Can I write Java without a class? A: No. Every piece of Java code must be inside a class. This is a fundamental OOP requirement.

Q: Why does Java need so much boilerplate (public class, main, etc.)? A: Java values explicitness and clarity over brevity. This verbosity helps large teams understand each other's code.

18. Summary

Every Java program lives inside a class. The main() method is the entry point. System.out.println() prints output. Semicolons terminate statements. Java is case-sensitive and follows strict naming conventions. Understanding this syntax foundation is critical for everything that follows.

19. Next Chapter Recommendation

You've mastered the syntax skeleton. In Chapter 4: Variables and Data Types, we'll learn how to store and manipulate different types of data — numbers, text, decimals, and booleans.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·