Want to start coding in Java? Check out our detailed Java Environment Setup Tutorial! This step-by-step guide walks you through downloading the JDK, installing it, and configuring everything you need to kickstart your Java programming journey. You can read the full tutorial here.
Introduction:
Starting with Java is exciting; not only is it flexible, but it is also efficient! Known for its versatility, Java supports various applications. In this guide, we will explore how to write your first java program and ensure you understand the basic syntax and structure right from the beginning.
Java Program Structure:
Every simple Java program follows a specific structure with essential components that help execute the code effectively. Here’s the basic syntax:
public class ClassName { public static void main(String[] args) { // Code to execute goes here } }
Let’s break down the components of this structure:
- Class Declaration: Every Java program has at least one class, and the class name must match the filename. For example, if the file is HelloWorld.java, then the class should be HelloWorld.
- Main Method: The main method is where Java programs start execution. You write it as
public static void main(String[] args)
. - Statements: Inside the main method, we add statements that define what the program should do. Essentially, the program executes statements in sequence, and you terminate each statement with a semicolon (;).Essentially
Writing Your First Java Program:
Now, let’s dive into the steps of how to write your first java program that prints “Hello, World!” to the screen.
Step 1: Create a New Java File
- Open a text editor or an IDE (like Visual Studio Code or IntelliJ IDEA).
- To begin, type the following code and then save the file as HelloWorld.java.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Explanation of the Code:
- Line 1: public class HelloWorld
- public: Similar to the class, this method is also public, meaning it can be called from outside the class.
- class: This means the method belongs to the class itself, not to instances of the class. Thus, it allows Java to call this method without creating an object of the class.
- HelloWorld: This is the name of the class. It can be any name, but in Java, class names conventionally start with a capital letter. Here, the class is named HelloWorld.
- Line 2: public static void main(String[] args)
- public: Similar to the class, this method is also public, meaning it can be called from outside the class.
- static: This means the method belongs to the class itself, not to instances of the class. It allows Java to call this method without creating an object of the class.
- void: This is the return type of the method. void means it doesn’t return any value.
- main: This is the name of the method. In Java, the main method is the entry point of any program, meaning it’s the first method that gets called when you run the program.
- (String[] args): This is the parameter for the main method. String[] is an array of strings, and args is the name of this array. It’s used to capture command-line arguments passed when the program is run.
- Line 3: System.out.println(“Hello, World!”);
- System: This is a built-in Java class that provides access to system resources.
- out: This is a static member of the System class and represents the standard output stream, usually the console.
- println: This is a method of System.out, which prints text to the console and moves the cursor to the next line.
- (“Hello, World!”): This is the argument passed to println. It’s a string, and it will be printed as it is.
Step 2: Compiling and Running Your Program:
After writing the code, you’ll see just how simple it is when you understand how to write your first java program. Once you’ve saved your file, follow these steps:
- Open Command Prompt or Terminal:
- Navigate to the directory where you saved HelloWorld.java.
- Compile the Program:
- In the command line, type:
- javac HelloWorld.java
- In the command line, type:
- This command compiles your Java code into bytecode, creating a HelloWorld.class file. If you find no errors, your code has compiled successfully.
- Run the Program:
- To execute the program, type:
- java HelloWorld
- To execute the program, type:
- You should see the output:
- Hello, World!
Basic Java Syntax Rules:
To avoid errors, you should remember these syntax rules as you learn how to write your first java program:
- Case Sensitivity: Java is case-sensitive; therefore, it treats System and system as different identifiers.
- Class Names: By convention, class names should start with an uppercase letter. If there are multiple words, use camel case (e.g., MyFirstClass).
- Method Names: Method names should start with a lowercase letter and use camel case (e.g., calculateSum).
- Statements End with Semicolons: Every statement in Java must end with a semicolon (;).
Common Mistakes to Avoid:
As you write your first Java program, keep in mind these common pitfalls to avoid:
- Forgetting the Semicolon: Each statement needs a semicolon at the end.
- Case Errors: Java is case-sensitive, so always check your capitalization.
- Mismatched Class and File Names: The filename should match the class name exactly.
Conclusion:
By following this guide, you now know how to write your first java program and can start building your skills in Java development. Writing and running your first Java program serves as a strong foundation to get comfortable with Java’s syntax and structure. Once you master these basics, try experimenting with small programs to reinforce your skills before diving into more complex concepts!
Interview Questions
1. Why is the main
method in Java static?(Google)
Because it allows the JVM to call it without creating an instance of the class.
2. Explain the role of the public
and void
keywords in the main
method.(Google)
public
makes the method accessible from anywhere, and void
indicates it does not return a value.
3. What happens if the main
method is written without static
?(Amazon)
The program will compile but throw a runtime error since the JVM needs main
to be static.
4. Can we have multiple main
methods in a single Java class?(Amazon)
Yes, by method overloading, but only the public static void main(String[] args)
is the entry point for execution.
5. What is the significance of String[] args
in the main
method?(Microsoft)
It allows the program to accept command-line arguments, accessible within the program.
Remember What You Learn!
Question
Your answer:
Correct answer:
Your Answers