Java Syntax and First Program
# 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, andmain.
-
Use
System.out.println()andSystem.out.print().
- Write single-line and multi-line comments.
- Build a basic calculator program.
3. Anatomy of a Java Program
Let's break down every single keyword:
| Component | Meaning |
|---|---|
public | Access modifier — this class/method is accessible from anywhere |
class | Keyword to declare a class (the blueprint) |
HelloWorld | The name of the class (must match the filename) |
static | The method belongs to the class itself, not to an instance/object |
void | The method does not return any value |
main | The entry point — JVM looks for this method to start execution |
String[] args | An 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.
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.
Output:
System.out.print() — Prints text but stays on the same line.
Output:
System.out.printf() — Formatted output (like C's printf).
Output:
6. Comments in Java
Comments are ignored by the compiler. They exist to explain your code to other developers (and your future self).7. Semicolons and Curly Braces
-
Every statement must end with a semicolon
;.
-
Blocks of code are wrapped in curly braces
{ }.
8. Escape Characters
Special characters inside strings:| Escape | Output |
|---|---|
\n | New line |
\t | Tab space |
\\ | Backslash |
\" | Double quote |
Output:
9. Java is Case-Sensitive
Java treats uppercase and lowercase letters as completely different:-
System≠system
-
String≠string
-
Main≠main
A single wrong capitalization will crash your program.
10. Naming Conventions
| Entity | Convention | Example |
|---|---|---|
| Class | PascalCase | StudentReport |
| Method | camelCase | calculateTotal() |
| Variable | camelCase | firstName |
| Constant | UPPERSNAKECASE | MAX_SIZE |
| Package | lowercase | com.myapp.utils |
11. Mini Project: Basic Calculator
Output:
12. Common Mistakes
-
Missing semicolon:
System.out.println("Hi")→ Compilation error.
-
Wrong class name: File is
Hello.javabut class isHelloWorld→ 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()notcs().
- Comment your code, but don't over-comment obvious lines.
14. Exercises
- 1. Write a program that prints your name, age, and city on three separate lines.
-
2.
Write a program using only
System.out.print()(notprintln) to print "Java is fun!" on one line using three separate print statements.
- 3. Create a program that demonstrates all four escape characters.
15. MCQ Quiz with Answers
What is the entry point of every Java application?
What does System.out.println() do?
<!-- comment -->
b) # comment
c) // comment
d) -- comment
Answer: c) // comment
Every Java statement must end with a:
Java is:
What naming convention do Java classes follow?
What does \t produce in a string?
Which keyword means the method doesn't return any value?
What does String[] args represent in the main method?
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(), andprintf()?
-
Q: Can a Java program have multiple classes? Can it have multiple
mainmethods?
- 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. Themain() 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.