Writing your first Java program is an exciting milestone in your programming journey. It helps you understand the basic structure of Java code, how programs execute, and how output is displayed. Learning to create and run a simple Java program builds a strong foundation for advanced Java programming and Android app development.
Introduction to Java Programming
Java is a powerful and object-oriented programming language used to develop desktop applications, web applications, Android apps, enterprise software, and cloud-based solutions.
Before writing your first Java program, make sure you have installed:
- Java Development Kit (JDK)
- Android Studio or another Java IDE
- Properly configured Java environment variables
Once your setup is complete, you are ready to create your first Java application.
Creating Your First Java Program
A Java program is written inside a class, and execution always starts from the main() method.
Basic Java Program Example
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output
Hello World
This simple program displays the text “Hello World” on the screen.
Understanding the Java Program Structure
Class Declaration
Every Java application contains at least one class.
public class Main
In this example:
publicis an access modifierclassis a Java keywordMainis the class name
The file name must match the class name.
Example:
Main.java
Main Method
The main method is the entry point of every Java application.
public static void main(String[] args)
Java starts program execution from this method.
Components of the Main Method
publicallows access from anywherestaticenables execution without creating an objectvoidindicates no return valuemainis the method nameString[] argsstores command-line arguments
Output Statement
Java uses System.out.println() to display information on the screen.
Example:
System.out.println("Welcome to Java");
This statement prints the specified text and moves the cursor to the next line.
Steps to Run Your First Java Program
Step 1: Create a Java File
Create a file named:
Main.java
Step 2: Write the Java Code
Enter the Java program into the file.
Step 3: Save the File
Save the file with the .java extension.
Step 4: Compile the Program
Open Command Prompt or Terminal and run:
javac Main.java
This command converts the source code into Java bytecode.
Step 5: Execute the Program
Run the compiled program using:
java Main
The output will appear on the screen.
Important Rules in Java Programming
File Name Must Match Class Name
If the class name is:
Main
The file name must be:
Main.java
Java is Case Sensitive
Java treats uppercase and lowercase letters differently.
Correct:
Main
Incorrect:
main
Statements End with Semicolons
Most Java statements must end with a semicolon.
Example:
int age = 20;
Missing semicolons can cause compilation errors.
Use Curly Braces Correctly
Curly braces define code blocks for classes and methods.
Example:
{
// code block
}
Why Writing Your First Java Program is Important
Creating your first Java application helps you:
- Understand Java syntax
- Learn program structure
- Practice compiling and executing code
- Gain confidence in programming
- Build a foundation for advanced concepts
It is the first step toward becoming a Java developer or Android app developer.
Common Beginner Mistakes
Many beginners encounter simple errors when writing their first Java program.
Common mistakes include:
- Missing semicolons
- Incorrect class names
- Wrong file extensions
- Typing mistakes
- Missing curly braces
- Incorrect capitalization
Carefully reviewing your code helps prevent these issues.
Real-World Importance of Java Programming
Java is used in many industries and technologies, including:
- Android App Development
- Web Development
- Enterprise Software
- Banking Systems
- E-commerce Applications
- Cloud Computing
- Educational Platforms
Learning Java opens opportunities to build professional software solutions.
Benefits of Learning Java
Java offers many advantages for beginners and professionals:
- Easy to learn
- Platform independent
- High industry demand
- Strong developer community
- Excellent career opportunities
- Powerful Android development support
These benefits make Java one of the most valuable programming languages to learn.
Conclusion
Writing your first Java program is the foundation of your programming journey. By understanding classes, methods, syntax, and program execution, you develop the skills needed to build more advanced applications. Consistent practice with simple Java programs will strengthen your coding abilities and prepare you for Android development, software engineering, and other exciting career opportunities in technology.