Skip to main content

💻 How to Write Your First Java Program – Step-by-Step Guide || FSJD-06-29-05



💻 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:

  1. Why is Java case-sensitive?

    Because it's based on C/C++ and allows more flexibility in naming.

  2. What is the role of the main() method?

    It’s the starting point of Java execution.

  3. 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.

  4. 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

Popular posts from this blog

What is Java? A Complete Beginner Guide to Java as a Language and Platform || FSJD-01-24-05

☕ What is Java? A Beginner-Friendly Guide to Java as a Language and Platform Are you just stepping into the world of programming? Or preparing for that first tech interview and trying to make sense of Java? Don’t worry — you're in the right place. Java isn’t just a coding language; it’s a whole ecosystem. In this beginner-friendly post, we’ll break down what Java really is — in simple terms — so you can start your learning journey with clarity and confidence. 🧠 So… What Exactly is Java? Java is both a programming language AND a platform . Yes — it’s more than just lines of code. Java provides a complete environment that lets you build applications for almost any device — from laptops to smartphones to smart TVs and more. 💻 Java as a Programming Language Java was created by James Gosling and his team at Sun Microsystems (now owned by Oracle ) and officially released in 1995 . Let’s break down what makes Java special as a language: ✅ High-Level Easy to read and wr...

☕ Understanding the Structure of a Java Program – Documentation || FSJD-07-29-05

☕ Understanding the Structure of a Java Program – A Beginner to Pro Guide “An investment in knowledge pays the best interest.” — Benjamin Franklin If you’re starting your journey in Java, the structure of a Java program is one of the first and most important things you should understand. Whether you're preparing for interviews or writing your first piece of code, a strong foundation can set you up for success. In this blog, we’ll break down the complete anatomy of a Java program — from the documentation section to the main() method — and explain each component in simple yet professional terms. 📋 Step-by-Step: How to Prepare Your First Java Program Before writing a Java program, follow these key steps: Download & Install Java Software Install the Java Development Kit (JDK) Choose an editor or IDE (like VS Code , IntelliJ , or Eclipse ) Understand the Java Program Structure Learn Java naming conventions Understand different sections o...

🔰 Java Software Setup Guide for Beginners || FSJD-05-29-05

  🔰 Java Software Setup Guide for Beginners By: Tahir | For Interview Prep & Fresh Learning “An investment in knowledge pays the best interest.” – Benjamin Franklin Whether you’re starting out with Java or brushing up for interviews, setting up the right environment is your first and most important step. In this post, I’ll walk you through the essential tools and software needed to start coding in Java, using simple language and clear concepts. 🚀 What Do You Need to Start Java Programming? To begin Java development, you need three main things: 1. JDK (Java Development Kit) The JDK is the core software you need to write, compile, and run Java programs. ✅ What Is JDK? The Java Development Kit is a package of tools that allows developers to write Java programs. It includes: A compiler ( javac ) to convert Java code into bytecode The Java Runtime Environment (JRE) to run the bytecode Development tools and libraries 📌 Important JDK Versions: Java S...