Lesson 1.7: Writing Your First Java Program
Introduction
Now that your Java development environment is ready, it’s time to write your first Java program.
Every programmer starts with a simple program called “Hello, World!”. Although it is a small program, it introduces the basic structure of every Java application.
In this lesson, you will write, compile, and run your first Java program while understanding every line of code.
Learning Objectives
After completing this lesson, you will be able to:
-
Write your first Java program.
-
Understand the structure of a Java program.
-
Explain the purpose of each line of code.
-
Compile and run a Java program.
-
Follow Java naming conventions.
Your First Java Program
Create a file named Hello.java and type the following code:
public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Save the file.
Output
Hello, World!
Understanding the Program Line by Line
Line 1
public class Hello
Explanation
-
public → An access modifier that makes the class accessible from anywhere.
-
class → A keyword used to define a class.
-
Hello → The name of the class.
Note: The file name must be Hello.java because the class name is Hello.
Line 2
{
The opening curly brace { marks the beginning of the class.
Line 3
public static void main(String[] args)
This is called the main method.
It is the starting point of every Java application.
Whenever a Java program runs, the JVM first looks for the main() method.
Understanding Each Keyword
public
Allows the JVM to access the method.
static
Allows the method to run without creating an object of the class.
void
Means the method does not return any value.
main
The predefined method name where program execution begins.
String[] args
Stores command-line arguments passed to the program.
Line 4
{
The opening brace of the main() method.
Everything inside this block will be executed.
Line 5
System.out.println("Hello, World!");
This statement prints text on the screen.
Understanding Each Part
System
A predefined Java class.
out
Represents the standard output stream (the console).
println()
Prints text and moves the cursor to the next line.
“Hello, World!”
This is a String Literal.
Everything inside double quotation marks is treated as text.
Line 6
}
Closes the main() method.
Line 7
}
Closes the class.
Program Flow
When you run the program:
-
JVM loads the class.
-
JVM finds the
main()method. -
The statements inside
main()execute from top to bottom. -
The text Hello, World! is displayed.
-
The program ends.
Using Different Output Statements
Example 1
System.out.println("Welcome to Java");
Output
Welcome to Java
Example 2
System.out.println("My Name is Rahul"); System.out.println("I am learning Java.");
Output
My Name is Rahul
I am learning Java.
Example 3
System.out.print("Hello "); System.out.print("Students");
Output
Hello Students
Difference
print()
-
Prints output on the same line.
println()
-
Prints output and moves to the next line.
Escape Characters
Java provides special characters called escape sequences.
| Escape Sequence | Meaning |
|---|---|
\n |
New Line |
\t |
Tab Space |
\" |
Double Quote |
\\ |
Backslash |
Example
public class EscapeExample { public static void main(String[] args) { System.out.println("Java\nProgramming"); } }
Output
Java
Programming
Common Mistakes Made by Beginners
Incorrect File Name
hello.java
Class
public class Hello
This causes a compilation error because the file name and class name must match.
Missing Semicolon
Incorrect
System.out.println("Hello")
Correct
System.out.println("Hello");
Missing Curly Braces
Always close every opening { with a closing }.
Wrong Capitalization
Incorrect
system.out.println("Hello");
Correct
System.out.println("Hello");
Java is case-sensitive.
Best Practices
-
Use meaningful class names.
-
Keep proper indentation.
-
Always end statements with a semicolon (
;). -
Save the file before running it.
-
Match the file name with the public class name.
Key Points to Remember
-
Every Java program starts from the
main()method. -
Java is case-sensitive.
-
The file name and public class name must be the same.
-
System.out.println()prints text on the console. -
Statements end with a semicolon (
;).
Lesson Summary
In this lesson, you learned:
-
How to write your first Java program.
-
The structure of a Java program.
-
The purpose of the
main()method. -
How
System.out.println()works. -
The difference between
print()andprintln(). -
Common programming mistakes and how to avoid them.
You have now successfully written and understood your first Java program.
Practice Questions
-
What is the purpose of the
main()method? -
What does the
publickeyword mean? -
Why is the
statickeyword used in themain()method? -
What is the difference between
print()andprintln()? -
Why must the file name match the public class name?
-
What is an escape sequence?
Programming Exercise
Write Java programs to:
-
Print your name.
-
Print your college name.
-
Print your address.
-
Print three lines using
println(). -
Print two words on the same line using
print(). -
Use
\nand\tin a program.
Assignment
Create a Java program that displays the following:
Name : Your Name
College : Your College Name
Department: Computer Science
Course : Core Java Programming
Use proper formatting with println() and escape characters.