Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
The very first program that any Java programmer learns to code is Hello World Program in Java. But many a time we miss out on the nitty-gritty of the basic syntax. Through the medium of this article, I will get into the details of the Hello World Program in Java.
Below are the topics covered in this article:
Let’s get started.
Before we get into the details, lets first start with the coding and see how a basic Hello World program in Java is coded.
public class HelloWorldDemo { public static void main(String[] args) { System.out.println( "Hello World!" ); System.exit( 0 ); //success } }
Now that you are done with the coding, lets now analyze the program’s syntax in depth.
Line 1: public class HelloWorldDemo {
This line makes use of the keyword class for declaring a new class called HelloWorldDemo. Since Java is an Object-Oriented Programming (OOP) Language, the entire class definition, including all of its members must be contained in between the opening curly brace { and the closing curly brace}. Also, it is using the public keyword to specify the accessibility of the class from outside the package.
Line 2: public static void main( String[] args ) {
This line declares a method called main(String[]). It is called the main method and acts as the entry point for the Java compiler to begin the execution of the program. In other words, whenever any program is executed in Java, the main method is the first function to be invoked. Other functions in the application are then invoked from the main method. In a standard Java application, one main method is mandatory to trigger the execution.
Lets now break down this entire line and analyze each word:
public: it is an access modifier specifies the visibility. It allows JVM to execute the method from anywhere.
static: It is a keyword which helps in making any class member static. The main method is made static as there is no need for creating an object to invoke the static methods in Java. Thus, JVM can invoke it without having to create an object which helps in saving the memory.
void: It represents the return type of the method. Since the Java main method doesn’t return any value its return type is declared as void.
main(): It is the name of the method that has been configured in the JVM.
String[]: It represents that the Java main method can accept a single line argument of the type String array. This is also known as java command line arguments. Below I have listed down a number of valid java main method signatures:
Line 3: System.out.println( “Hello World!” );
System: It is a pre-defined class in java.lang package which holds various useful methods and variables.
out: It is a static member field of type PrintStream.
println: It is a method of PrintStream class and is used for printing the argument that has been passed to the standard console and a newline. You can also use print() method instead of println().
Line 4: System.exit( 0 );
The java.lang.System.exit() method is used to exit the current program by terminating the currently executing Java Virtual Machine. This method takes a status code as input which is generally a non-zero value. It indicates in case any abnormal termination occurs.
So that was all about the program syntax. Let’s now see how to compile Hello World in Java program.
Now what you need to is type in this program in your text editor save it with the class name that you have used in your program. In my case, I will be saving it as HelloWorldDemo.java.
Next step is to, go to your console window and navigate to the directory where you have saved your program.
Now in order to compile the program type in the below command:
javac HelloWorldDemo.java
Note: Java is case-sensitive, thus make sure that you type in the file name in the correct format.
If successfully executed, this command will generate a HelloWorldDemo.class file which will be machine independent and portable in nature.
Now that you have successfully compiled the program, let us try to execute our Hello World Program in Java and get the output.
In order to execute your HelloWorld in Java program on the command line, all you need to do is type in the below code:
java HelloWorldDemo
Voila! You have successfully executed your first program in Java.
In case you are using an IDE, you can skip all this hassle and just press the execute button in your IDE to compile and execute your Hello World in Program Java.
This brings us to the end of this article on Hello World Program in Java. If you want to know more about Java you can refer to our other Java Blogs.
Now that you have understood what is a Hello World Program in Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course are designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
For more check out these Java interview questions for experienced.
Got a question for us? Please mention it in the comments section of this “Hello World Program in Java” article and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co