Hello World In Java

Hello World In Java

Here is an example of a “Hello World” program in Java:

				
					public class HelloWorld {
   public static void main(String[] args) {
System.out.println("Hello, World!");
   }
}

				
			
  1. public: This is an access modifier that defines the visibility of the class. In this case, the class is defined as public, which means that it can be accessed from anywhere.
  2. class: This keyword is used to define a class in Java.
  3. HelloWorld: This is the name of the class. The name of the class should match the name of the file in which it is defined.
  4. public static void main(String[] args): This is the main method of the program, which is the entry point of the program. It is defined as public, which means that it can be accessed from anywhere. It is also defined as static, which means that it can be called without creating an instance of the class. The main method takes an array of strings as its argument.
  5. System.out.println(“Hello, World!”);: This statement prints the message “Hello, World!” to the console. The System.out object is an instance of the PrintStream class that is defined in the java.lang package. The println method is used to print the message and add a newline character at the end.

To run this program, you can save it as a file named HelloWorld.java, open a terminal or command prompt, navigate to the directory where the file is saved, and type the following command:

javac HelloWorld.java

This command will compile the Java code into bytecode. Once the code is compiled, you can run the program by typing:

java HelloWorld:

This command will execute the HelloWorld class and output the text “Hello, World!” to the console.

Join To Get Our Newsletter
Spread the love