Basic Building Blocks of a Java Program
Introduction:
A Java program is made up of various components that define its logic, structure, and behavior. These are the essential building blocks you need to understand to write effective Java code.
- Keywords
- Identifiers
- Data Types
- Variables
- Literals
- Operators
- Statements
- Control Flow Statements
- Comments
- Indentation & Formatting
- Class
- Method
- Objects
- Packages
1. Keywords
Reserved words that have predefined meanings in Java. You cannot use them as identifiers.
Example: public, class, static, void, if, else
2. Identifiers
Names given to variables, classes, methods, etc. They help uniquely identify elements in a program.
Example: Student, main, totalMarks
Rules: Must not start with a digit, no special characters (except _, $)
3. Variables
Variables are named containers that store data in memory (RAM) during program execution. They allow programs to read, update, and manipulate values.
Example:
int a = 100; // Declaration + Initialization
a = 150; // Assignment
System.out.println(a); // Using the variable
4. Data Types
Define the kind of data a variable can store.
- Primitive: int, float, char, boolean
- Non-primitive: String, Array, Object
5. Literals
Fixed values used in code — numbers, characters, strings, etc.
Example: 10, 'A', "Hello", true, 3.14
6. Operators
Symbols used to perform operations on data, such as arithmetic, logical, relational, etc.
Example: +, -, *, /, ==, &&
7. Statements
Instructions that Java executes, ending with a semicolon (;).
Examples:
System.out.println("Hello");
int x = 10;
8. Control Flow Statements
These include decision-making and looping constructs used to control the execution flow of a program.
- Conditional: if, else, else if, switch
- Looping: for, while, do-while
- Jumping: break, continue, return
9. Comments
Non-executable lines used to describe what the code is doing. Helps in understanding code logic.
Types:
- // Single-line
- /* */ Multi-line
- /** */ Documentation
10. Indentation & Formatting
Improves code readability and clean structure. While ignored by the compiler, consistent indentation helps maintain code.
Tip: Use 2 or 4 spaces consistently per block.
11. Class
- The base unit of every Java program.
- Defines structure and behavior via variables and methods.
public class MyClass {
// code
}
12. Method
- A block of code that performs a task.
- The main method is the entry point.
public static void main(String[] args) {
// execution starts here
}
13. Objects
- An object is an instance of a class.
- It has state (variables) and behavior (methods).
Student s = new Student();
14. Packages
- Used to organize related classes together.
- Helps avoid naming conflicts and improves structure.
package myproject;
Understand with Example:
A Java program is made up of several small components like keywords, identifiers, operators, comments, literals, etc. Let’s look at a simple example to understand how all of them come together.
// This program calculates the total and average marks of a student
package studentApp; // Package declaration
public class StudentMarks { // 'class' is a keyword; 'StudentMarks' is an identifier
public static void main(String[] args) { // 'public', 'static', 'void' are keywords
// Variable declarations with data types
int math = 85;
int science = 90;
int english = 88;
// Using operators to calculate total and average
int total = math + science + english;
double average = total / 3.0;
// Output statements (Statements + Literals + Objects)
System.out.println("Total Marks = " + total);
System.out.println("Average Marks = " + average);
}
}
What This Program Shows
| Building Block | Shown in Example |
|---|---|
| Keywords | public, class, static, void, int, double, package |
| Identifiers | StudentMarks, math, science, total, main, studentApp |
| Variables | math, science, english, total, average |
| Data Types | int, double |
| Literals | 85, 90, 88, 3.0, "Total Marks = " |
| Operators | +, /, = |
| Statements | Variable declarations, assignments, method calls ending with ; |
| Control Flow | None shown directly, but could include if-else, loops |
| Comments | // used throughout to explain code |
| Indentation & Format | Proper indentation for class, method, and logic |
| Class | public class StudentMarks { ... } |
| Method | public static void main(String[] args) |
| Objects | System.out is an object of PrintStream |
| Packages | package studentApp; at the top |
Summary:
| S.No. | Building Block | Description |
|---|---|---|
| 1 | Keywords | Reserved words with special meaning |
| 2 | Identifiers | Names for classes, variables, methods |
| 3 | Variables | Hold data during program execution |
| 4 | Data Types | Specify variable types |
| 5 | Literals | Fixed constant values |
| 6 | Operators | Perform operations on values/variables |
| 7 | Statements | Single executable instructions ending with ; |
| 8 | Control Flow Statements | if-else, switch, loops, break, continue, etc. |
| 9 | Comments | Explain code; ignored by compiler |
| 10 | Indentation & Formatting | Improves readability |
| 11 | Class | Blueprint of Java program |
| 12 | Method | Performs task; main method starts program |
| 13 | Objects | Instances of classes |
| 14 | Packages | Groups related classes into folders |