Hello World!
Certainly! The "Hello, World!" program is often the first program you write when learning a new programming language. Here's how you can create a simple "Hello, World!" program in Java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Explanation:
public class HelloWorld: This declares a class namedHelloWorld. In Java, the name of the class must match the name of the file (excluding the.javaextension). In this case, the file should be namedHelloWorld.java.public static void main(String[] args): This is the main method. It is the entry point of the program, and it's where the program execution begins. It takes an array of strings (args) as input, which can be used to pass command-line arguments to the program.System.out.println("Hello, World!");: This line of code prints "Hello, World!" to the console. TheSystem.out.println()method is used to display text, and it automatically adds a newline character after the text, so each call toprintlnresults in a new line.
To run this program:
Save the code above into a file named
HelloWorld.javaOpen a command prompt or terminal window.
Navigate to the directory where you saved the
HelloWorld.javafile.Compile the Java source code by running:
javac HelloWorld.javaAfter a successful compilation, run the program with the following command:
java HelloWorldYou should see the output "Hello, World!" displayed in the terminal.
That's it! You've created and run a simple "Hello, World!" program in Java.
Last updated
Was this helpful?