Last Updated on December 27, 2022 by Prepbytes
One of the most well-liked and often-used platforms and programming languages is Java. Java is quick, dependable, and safe. From PCs to online apps, scientific supercomputers to game consoles, and mobile phones to the Internet, Java is utilized everywhere.
The syntax of Java is straightforward and basic, making it simple to learn. It is built on C++, making it simpler for programmers who are familiar with C++.
Java programming may be broken down into three simple steps:
- The code for the program is written in a text editor and saved as HelloWorld.java.
- Enter "java HelloWorld.java" to compile it in the terminal window.
- Enter "java HelloWorld" to run (or execute) it in the terminal window.
The simplest Java program is the one that prints "Hello World" on the screen. It is provided below. Let’s attempt to decipher each piece of code one by one.
// This is a simple Java program.
// FileName: "HelloWorld.java".
Java Code for Hello Word Program
class HelloWorld { // Your program begins with a call to main(). // Prints "Hello, World" to the terminal window. public static void main(String args[]) { System.out.println("Hello World!"); } }
Output: Hello World!
Time Complexity: O(1)
Space Complexity: O(1)
"Hello World!" The HelloWorld class declaration, the main function, and the source code comments make up the program’s three main parts. Your grasp of the code will be fundamental after reading the following explanation:
1.Class definition
This line uses the keyword class to declare that a new class is being defined.
class HelloWorld
{
//
//Statements
}
2. HelloWorld
The class name is a part of the identifier. The space between the opening curly brace "{" and the closing curly brace "}" will contain the complete class definition, including all of its members.
3. main method:
The main method is a required component of every Java program. The main function (method), which is a must in a Java program, serves as the application’s entry point. whose Java signature is:
void public static main (String[] args)
- public: So that JVM can execute the method from anywhere.
- static: The main method is to be called without an object. The modifiers public and static can be written in either order.
- void: The main method doesn’t return anything.
- main(): Name configured in the JVM. The main method must be inside the class definition. The compiler executes the codes starting always from the main function.
- String[]: The main method accepts a single argument, i.e., an array of elements of type String.
Similar to C/C++, the main method serves as the application’s entry point and calls all other methods that your program needs.
Here is the code for the next line. Take note that it takes place within the main() function.
System.out.println("Hello, World");
This line prints the phrase "Hello, World" on the screen, followed by a new line. By using the built-in println() function, the output is produced. Out is a variable of type output stream connected to the console, and The System is a predefined class that allows access to the system.
Comments
They can either be multiline or single-line comments.
// This is a simple Java program.
// Call this file "HelloWorld.java".
This is a single-line comment. This type of comment must begin with // as in C/C++. For multiline comments, they must begin with/ and end with /.
Important Points
- HelloWorld is the same as the name of the file and the class that the program defines (HelloWorld.java). There is no chance behind this. There can only be one public class in Java that includes the main() function, and all codes must be contained within classes.
- As a matter of convention, the main class’s name ought to coincide with the name of the file containing the program (a class that includes the main method).
- Every Java application needs a class declaration with the same name as the file (class name and file name should be the same).
Compiling the program
- After successfully setting up the environment, we can open a terminal in both Windows/Unix and go to the directory where the file – HelloWorld.java is present.
- Now, to compile the HelloWorld program, execute the compiler – javac, to specify the name of the source file on the command line, as shown:
java HelloWorld.java - The compiler creates a HelloWorld.class (in the current working directory) that contains the bytecode version of the program. Now, to execute our program, JVM(Java Virtual Machine) needs to be called using java, specifying the name of the class file on the command line, as shown:
java HelloWorld - This will print “Hello World” on the terminal screen.
Conclusion
We discussed the Java program Hello World in this post.
We started by writing the program, then disassembled it to comprehend each line of code that went into it.
In our discussion of Java, we covered classes, the main function, the use of strings, the System.out.println() line, and comments.
Other Java Programs
Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java