Getting Started with Java: Your First Program
Introduction
Java is a powerful and widely-used programming language known for its simplicity, portability, and versatility. In this tutorial, we'll guide you through creating and running your first Java program, commonly known as the "Hello, World!" program. By the end of this tutorial, you'll have a solid understanding of how to set up your Java development environment and write and execute basic Java code.
Creating Your First Java Program
Let's create a simple Java program that prints "Hello, World!" to the console:
// HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Understanding the Program
public class HelloWorld { ... }
: Defines a class namedHelloWorld
. In Java, every application must have at least one class with amain
method.public static void main(String[] args) { ... }
: Themain
method serves as the entry point for the Java program. It is where the program starts execution.System.out.println("Hello, World!");
: This line prints the string "Hello, World!" to the console. TheSystem.out.println
method is used to output text to the standard output stream.
Compiling and Running the Program
Save the File : Save the above Java code in a file named
HelloWorld.java
.Compile the Program : Open a terminal or command prompt, navigate to the directory containing
HelloWorld.java
, and run the following command to compile the program:Example in javajavac HelloWorld.java
If there are no errors in your code, this command will generate a bytecode file named
HelloWorld.class
.Run the Program : After successful compilation, run the following command to execute the program:
Example in javajava HelloWorld
You should see the output:
Example in javaHello, World!
Congratulations! You've successfully written and executed your first Java program.
Conclusion
In this tutorial, you learned how to create, compile, and run a simple Java program. While the "Hello, World!" program is basic, it serves as an essential first step in learning Java programming. From here, you can explore more advanced Java concepts and start building more complex applications. Happy coding!