💻 How to Write Your First Java Program – Step-by-Step Guide
By Tahir | Beginner-Friendly + Interview Focused
“An investment in knowledge pays the best interest.” – Benjamin Franklin
Starting Java programming can feel overwhelming — but if you take it one step at a time, it’s actually very easy and interesting. In this guide, I’ll walk you through how to set up and write your first Java program — from naming conventions to program structure to compiling and running your code.
🧱 Step-by-Step: How to Prepare Your First Java Program
Here are the 3 major steps to writing your first Java program:
✅ 1. Download & Install Java Software
Before writing code, install:
-
JDK (Java Development Kit)
-
Editor or IDE (e.g., Notepad++, Eclipse, NetBeans)
📌 See my previous post for complete setup instructions.
✅ 2. Create Your First Java Program
This part includes:
-
Following correct naming conventions
-
Understanding the structure of a Java program
-
Writing and explaining your first code
🔤 Java Naming Conventions
Java is a case-sensitive language, meaning:
int a = 10;
int A = 20;
Here, a and A are treated as two different variables.
✅ Best Practices:
-
Class names: Start with a capital letter (
MyProgram) -
Variables/methods: Start with lowercase, use camelCase (
studentName) -
Constants: Use all uppercase with underscores (
MAX_VALUE)
🧠 Interview Tip:
Naming conventions are part of Java best practices. Interviewers may ask how you write clean and readable code — following naming standards is the answer!
🧩 Structure of a Java Program
A Java program is divided into 5 key sections:
1. 📄 Documentation Section (Optional)
Used to describe the program — includes:
-
Author’s name
-
Date of creation
-
Description
-
Version info
✅ You write this section using comments.
Java supports three types of comments:
// Single-line comment
/* Multi-line
comment */
/**
* Documentation comment
* Often used to generate HTML docs using javadoc
*/
📌 What is an Annotation?
Introduced in JDK 1.5, annotations look like this:
@Override
public void run() { ... }
Annotations provide metadata about code and are not removed at compile or runtime (unlike comments).
📄 Generate Documentation in HTML
You can use this command:
javadoc FileName.java
🔍 Useful Java CMD Commands:
-
javap -version→ Shows installed JDK version -
javap packageName.ClassName→ Shows class bytecode details
2. 📦 Package Statement (Optional)
If you are using packages (folders for organizing classes), define them at the top of the program:
package myprogram;
3. 📥 Import Statement
Used to include built-in Java libraries:
import java.util.Scanner;
4. 🧱 Class/Interface Section
This section defines the structure of the program — where we declare:
-
Variables
-
Methods
-
Main class
Example:
public class HelloWorld {
int number; // variable
void display() { // method
System.out.println("Hello, Java!");
}
}
5. 🔁 Main Method Section (Execution starts here)
This is the entry point of every Java program:
public static void main(String[] args) {
System.out.println("Hello World!");
}
🔎 What Does It Mean?
-
public– accessible from anywhere -
static– method runs without creating object -
void– returns nothing -
main– the name recognized by JVM -
String[] args– for command-line input
🔃 Step 3: Compile and Run the Java Program
🛠️ Compile:
javac MyProgram.java
▶️ Run:
java MyProgram
✅ Note: File name and class name must match if the class is
public.
✅ Summary – What You Learned
| Step | Description |
|---|---|
| Install Java Tools | JDK + Editor or IDE |
| Follow Naming Conventions | Use camelCase, ClassNames with uppercase |
| Program Structure | 5 sections: Docs, Package, Import, Class, Main |
| Compile and Run | Use javac and java |
🧠 Interview Booster Questions
Here are a few questions you can expect in interviews:
-
Why is Java case-sensitive?
Because it's based on C/C++ and allows more flexibility in naming.
-
What is the role of the
main()method?It’s the starting point of Java execution.
-
What’s the difference between comments and annotations?
Comments are removed at compile time. Annotations are retained for metadata and used by tools like frameworks and JVM.
-
Can we write a Java program without a
main()method?Not in standalone applications. But in app servers or frameworks (like Spring), custom entry points can be defined.
Comments
Post a Comment