First Java 8 Program
Overview
In this tutorial, we will create and run our first Java 8 program. We will write a simple "Hello, World!" application, compile it, and run it. This will help you get started with Java programming.
Step-by-Step Guide
1. Create a Java Source File
Create a new file with a .java
extension. For this example, we will name it HelloWorld.java
.
2. Write the Java Program
Open the HelloWorld.java
file in a text editor or an Integrated Development Environment (IDE) and write the following code:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Understanding the Code
Let's break down the code:
public class HelloWorld
: This line declares a public class namedHelloWorld
. In Java, every application must have at least one class definition.public static void main(String[] args)
: This line declares themain
method, which is the entry point of any Java application. Themain
method must be declaredpublic
,static
, and must returnvoid
. It accepts a single parameter, an array ofString
objects.System.out.println("Hello, World!");
: This line prints the string"Hello, World!"
to the console.System.out
is a standard output stream, andprintln
is a method that prints a line of text to the console.
3. Compile the Java Program
Open a terminal or command prompt and navigate to the directory where you saved HelloWorld.java
. Run the following command to compile the program:
javac HelloWorld.java
If there are no errors, this command will generate a file named HelloWorld.class
in the same directory. This file contains the bytecode of the program, which can be executed by the Java Virtual Machine (JVM).
4. Run the Java Program
To run the compiled Java program, use the following command:
java HelloWorld
You should see the following output in the terminal or command prompt:
Hello, World!
Conclusion
Congratulations! You have successfully written, compiled, and executed your first Java 8 program. This is a fundamental step in learning Java programming, and you are now ready to explore more advanced concepts and features of Java 8.