📘LESSON 8
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Identify Java source files.
- Explain Java syntax rules.
- Write basic Java program structures.
- Identify important syntax elements in Java.
- Explain the importance of proper Java syntax.
3.2 Overview
Java programs are written inside Java source files using specific syntax rules and structures. Java syntax defines how programs must be written so that they can compile and execute correctly.
This lesson introduces learners to Java files and syntax and explains the rules used to structure Java programs correctly.
Java syntax is important in:
- software development,
- enterprise applications,
- Android development,
- web applications,
- and programming environments.
Understanding Java syntax is important because syntax errors prevent Java programs from compiling and running successfully.
KT0801 — Java Files
Java source code is stored inside files with the:
.java
extension.
Each Java file normally contains:
- classes,
- methods,
- variables,
- and program logic.
Example Java File
Main.java
Structure of a Java File
| Component | Purpose |
|---|---|
| Package Statement | Organizes classes |
| Import Statement | Imports libraries |
| Class Declaration | Defines program class |
| Methods | Performs tasks |
Example Java File
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Importance of Java Files
Java files help developers:
- organize source code,
- manage applications,
- and structure programs correctly.
KT0802 — Java Syntax
Java syntax refers to the rules used when writing Java programs.
Programs must follow proper syntax to:
- compile successfully,
- execute correctly,
- and remain readable.
Basic Java Syntax Rules
| Rule | Description |
|---|---|
| Statements end with semicolons | ; terminates statements |
| Code blocks use braces | {} defines blocks |
| Java is case-sensitive | Uppercase/lowercase matter |
| Classes use class keyword | Defines classes |
Example of Java Syntax
public class Main {
public static void main(String[] args) {
int number = 10;
System.out.println(number);
}
}
Important Syntax Elements
| Element | Purpose |
|---|---|
| class | Defines a class |
| main() | Entry point |
| System.out.println() | Displays output |
| ; | Ends statements |
| {} | Defines code blocks |
Java Comments
Comments are notes written inside programs for explanation purposes.
Single-Line Comment
// This is a comment
Multi-Line Comment
/*
This is
a multi-line comment
*/
Importance of Java Syntax
Proper syntax supports:
- successful compilation,
- readable code,
- easier debugging,
- and software quality.
3.5 Key Notes / Summary
- Java source files use the
.javaextension. - Java programs follow specific syntax rules.
- Java is case-sensitive.
- Statements end with semicolons.
- Braces define code blocks.
- Comments improve readability and documenta