Programming and Java Fundamentals
Understanding the First Java Program
আপনি একটি free preview lesson দেখছেন।
Lesson Overview
In the previous lesson, we created and ran the following Java program:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Although the program is small, it contains several fundamental building blocks of a Java program.
In this lesson, we will understand:
- What a class is
- What
public class Mainmeans - The role of the
mainmethod - The basic meaning of
public,static, andvoid - Why
String[] argsis present - What statements and code blocks are
- What
System.out.println()does - Where Java program execution begins
Learning Objectives
After completing this lesson, you will be able to:
- Explain the structure of a basic Java program
- Identify a class declaration
- Understand the role of the
mainmethod - Explain the basic meaning of
public,static, andvoid - Identify statements and code blocks
- Display output using
System.out.println() - Understand the roles of semicolons and braces
- Explain the execution order of a basic program
Our First Program
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Output:
Hello Java
Program structure:
Main class
└── main method
└── output statement
Now let's look at each part.
public class Main
The first line is:
public class Main {
This is a class declaration.
Here:
public— access modifierclass— Java keywordMain— class name{— beginning of the class body
What Is a Class?
A class is a fundamental structure in a Java program.
A class can organize data and behavior.
In our first program:
public class Main {
}
Main is a class.
Later, we will create classes such as:
Student
Course
BankAccount
Order
GradeCalculator
When we learn object-oriented programming, we will understand the actual role of classes in much more detail.
For now, remember:
Java code is generally organized inside classes.
The class Keyword
class
is a reserved Java keyword.
It tells the compiler that we are declaring a class.
Example:
class Student {
}
Keywords have meanings defined by the language. Therefore, a keyword cannot normally be used as a variable or class name.
Class Name: Main
Main
is the name of the class.
Main is not a special or mandatory class name.
The following is also valid:
public class Application {
public static void main(String[] args) {
System.out.println("Application started");
}
}
In this case, the source file would be named:
Application.java
The name of a public class and the source file name must match.
In our case:
Class: Main
File: Main.java
Class Naming Convention
Java class names generally follow the PascalCase convention.
Good examples:
Main
Student
Course
BankAccount
GradeCalculator
When a name contains multiple words, each word starts with a capital letter:
GradeCalculator
BankAccount
CourseEnrollment
public
public class Main
Here, public is an access modifier.
Access modifiers help control where a class or member can be accessed from.
For now, remember:
publicindicates wider accessibility.
We will study access modifiers in detail when we learn OOP.
Curly Braces { }
The class body is placed inside curly braces:
public class Main {
}
The opening brace:
{
starts a block.
The closing brace:
}
ends a block.
Our program contains two nested blocks:
public class Main { // Class block starts
public static void main(String[] args) { // Method block starts
System.out.println("Hello Java");
} // Method block ends
} // Class block ends
Structure:
Class
{
Method
{
Statement
}
}
The main Method
Now look at the second important line:
public static void main(String[] args) {
This is the declaration of the main method.
A basic Java application usually begins execution from this method.
The method name is:
main
The traditional Java application entry point is:
public static void main(String[] args)
What Is a Method?
A method is a named block of related instructions.
In our program:
public static void main(String[] args) {
System.out.println("Hello Java");
}
Method name:
main
Method body:
{
System.out.println("Hello Java");
}
Later, we will learn how to create our own methods.
Why Is main Important?
When a Java application is started, the runtime looks for a suitable main method and begins execution from there.
Example:
public class Main {
public static void main(String[] args) {
System.out.println("First");
System.out.println("Second");
}
}
Execution:
Enter main
↓
Print First
↓
Print Second
↓
Exit main
Output:
First
Second
public static void
At the beginning of the main method, we have:
public static void
For now, understanding the basic meaning of each part is enough.
public
public
The method can be accessible from outside when the application starts.
static
static
Because main is static, an object of the Main class does not need to be created before the main method can run.
For now, remember:
The
mainmethod is directly available through the class for application startup.
We will understand static in more depth after learning about classes and objects.
void
void
is the return type of the method.
void means that the method does not return a value.
Our main method runs the program but does not return a result to its caller.
String[] args
Inside the parentheses of the main method, we have:
String[] args
This is a parameter used to receive command-line arguments.
Here:
String[]
is the parameter type.
args
is the parameter name.
At this stage, you do not need to use arrays or command-line arguments.
You only need to recognize the standard main method signature:
public static void main(String[] args)
We will learn about String, arrays, and parameters in later lessons.
Parentheses ( )
Parentheses appear after a method name:
main(...)
In our case:
main(String[] args)
Parentheses can contain a method's input or parameter declaration.
Parentheses are also used when calling a method:
println(...)
This concept will become clearer when we learn methods.
Method Body
The instructions of the main method are placed inside curly braces:
public static void main(String[] args) {
}
Our method body is:
{
System.out.println("Hello Java");
}
When the program executes, the statements inside the body run.
What Is a Statement?
Look at this line:
System.out.println("Hello Java");
This is a statement.
A statement is a complete instruction in a program.
Some other examples of statements are:
int age = 30;
int total = 10 + 20;
We will learn about these in later lessons.
For now, our statement:
System.out.println("Hello Java");
displays a message in the console.
Semicolon ;
At the end of the statement, we have:
;
Complete statement:
System.out.println("Hello Java");
Simple Java statements generally end with a semicolon.
Wrong:
System.out.println("Hello Java")
Correct:
System.out.println("Hello Java");
If the semicolon is missing, a compilation error will occur.
System.out.println()
System.out.println("Hello Java");
This statement sends output to the console.
Output:
Hello Java
At a high level, you can think of it as three parts:
System
↓
out
↓
println(...)
Now let's look at the basic meaning of each part.
System
System
is a class in the Java standard library.
out
System.out
represents standard output.
In our examples, the output appears in the console.
println
println(...)
is a method that prints a value and then moves to a new line.
Example:
System.out.println("Java");
System.out.println("Foundation");
Output:
Java
Foundation
"Hello Java"
"Hello Java"
is a text value.
In Java, text written inside double quotes is called a String literal.
Examples:
"Java"
"Hello Java"
"Java and OOP Foundation"
If you remove the double quotes, Java will not treat it as text.
Wrong:
System.out.println(Hello Java);
Correct:
System.out.println("Hello Java");
What Is the Dot . Doing?
In this expression:
System.out.println
the dot is used to access one member from another.
Conceptually:
System
└── out
└── println()
You will see this kind of member access many times in Java code.
For now, just recognize the syntax. Its meaning will become clearer after we learn about objects and methods.
Java Is Case-Sensitive
Java treats uppercase and lowercase letters as different.
Correct:
System.out.println("Hello");
Wrong:
system.out.println("Hello");
Wrong:
System.Out.Println("Hello");
because:
System ≠ system
out ≠ Out
println ≠ Println
Similarly:
Main
and:
main
are not the same identifier.
Indentation
The Java compiler does not determine program structure from indentation, but indentation is extremely important for readable code.
Recommended:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Avoid writing code like this:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
The second version may compile, but it is difficult to read.
Execution Order
Look at the following program:
public class Main {
public static void main(String[] args) {
System.out.println("Step 1");
System.out.println("Step 2");
System.out.println("Step 3");
}
}
Statements inside the main method generally execute from top to bottom.
Output:
Step 1
Step 2
Step 3
If the order of the statements changes, the order of the output will also change.
Complete Program Breakdown
Let's look at the complete program again:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
| Code | Meaning |
|---|---|
public | Access modifier |
class | Keyword used to declare a class |
Main | Class name |
{ } | Code block |
static | Makes the class-level method available without creating an object |
void | The method does not return a value |
main | Application entry method |
String[] args | Parameter used to receive command-line arguments |
System | Java standard library class |
out | Standard output |
println | Method used to print output |
"Hello Java" | String literal |
; | Statement terminator |
Common Structural Errors
Missing Semicolon
Wrong:
System.out.println("Hello Java")
Correct:
System.out.println("Hello Java");
Missing Quote
Wrong:
System.out.println("Hello Java);
Correct:
System.out.println("Hello Java");
Missing Brace
Wrong:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
Correct:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Wrong Capitalization
Wrong:
system.out.println("Hello Java");
Correct:
System.out.println("Hello Java");
Incorrect main Name
Wrong:
public static void Main(String[] args) {
}
Correct:
public static void main(String[] args) {
}
Java is case-sensitive.
An Important Point
Not every Java class needs a main method.
Example:
public class Student {
}
This is a valid class.
A main method is needed only in a class that we want to use as an application entry point.
A large application may contain many classes, but not all classes are application entry points.
Important Terms
Class
A structure used to organize Java code and related data or behavior.
Class Declaration
The syntax used to define a class.
public class Main {
}
Keyword
A reserved word in the Java language.
Examples:
public
class
static
void
Method
A named block that contains instructions.
main Method
The entry point of a basic Java application.
Statement
A complete instruction in a program.
Code Block
Code contained inside curly braces { }.
String Literal
A text value written inside double quotes.
Semicolon
Indicates the end of a simple statement.
Standard Output
The normal output destination of a program. In our examples, this is the console.
Practice Exercise 1: Identify the Parts of the Program
Look at the following program:
public class CourseApplication {
public static void main(String[] args) {
System.out.println("Course started");
}
}
Identify:
- Class name
classkeyword- Method name
- Return type
- Parameter
- Output statement
- String literal
- Statement terminator
Practice Exercise 2: Predict the Output
Before running the program, write the expected output:
public class Main {
public static void main(String[] args) {
System.out.println("Java");
System.out.println("OOP");
System.out.println("Foundation");
}
}
Practice Exercise 3: Fix the Error
Fix the following program:
public class Main {
public static void main(String[] args) {
System.out.println("Java")
System.out.println("Foundation");
}
}
Expected output:
Java
Foundation
Practice Exercise 4: Fix the Brace Structure
Fix the following code:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
Practice Exercise 5: Write Your Own Program
Create the following output:
Course: Java and OOP Foundation
Java Version: 21
Status: Learning Java
Write a complete Java program using a Main class and a main method.
Knowledge Check
Question 1
What is a Java class?
Question 2
What does the class keyword do?
Question 3
If the name of a public class is Main, what should the source file be named?
Question 4
Which method is the entry point of a basic Java application?
Question 5
What do you need to remember about static at this stage?
Question 6
What does void mean?
Question 7
What is String[] args used for?
Question 8
What is a statement?
Question 9
Which symbol generally ends a simple Java statement?
Question 10
What does System.out.println() do?
Question 11
What does it mean that Java is case-sensitive?
Question 12
Does every Java class need a main method?
Knowledge Check Answers
Answer 1
A class is a fundamental structure used to organize Java code and related data or behavior.
Answer 2
The class keyword is used to declare a class.
Answer 3
The source file should be named:
Main.java
Answer 4
A basic Java application usually begins execution from:
public static void main(String[] args)
Answer 5
Because main is static, an object of the Main class does not need to be created before the main method can run.
Answer 6
void means that the method does not return a value.
Answer 7
String[] args is a parameter used to receive command-line arguments.
Answer 8
A statement is a complete instruction in a program.
Answer 9
A simple statement generally ends with a semicolon:
;
Answer 10
System.out.println() prints a value to the console and moves to a new line afterward.
Answer 11
Java treats uppercase and lowercase letters as different.
For example:
Main
main
are not the same identifier.
Answer 12
No. Only a suitable entry point is needed to start application execution. Other classes do not need to contain a main method.
Lesson Summary
In this lesson, we learned:
- Java code is generally organized inside classes
- The
classkeyword declares a class - The name of a public class and its source file must match
- Curly braces define class and method bodies
- A basic Java application begins execution from the
mainmethod - Because
mainisstatic, an object does not need to be created before it runs voidmeans that a method does not return a valueString[] argsis a parameter for receiving command-line arguments- A statement is a complete instruction
- Simple statements generally end with a semicolon
System.out.println()displays output in the console- String literals are written inside double quotes
- Java is case-sensitive
- Statements inside a method generally execute from top to bottom