Introduction to Java also begins with some important facts that any beginner needs to know about a Java program. This part I’ll try to explain the elemental parts of Java programming language by breaking down the simplest program in the world – “Hello World”.
The "Hello World" Program
Here's a basic "Hello World" program in Java.
Components of the Program
Class Declaration
public:
It refers to this an access modifier which enables the class to be accessed by other classes.
class:
This keyword is specifically applied in Java to declare a class.
HelloWorld:
This is the name of class. Ideally, it should be the name of the file in exact form (HelloWorld. java).
Main Method
public:
Once again, this is an access modifier and by using it we can make the main method accessible throughout the java program.
static:
This keyword implies that the method operates on the behalf of the class, not an instance of the class. It can be instantiated without having the class in which it was defined.
void:
This means that the method is designed to return nothing, thus not having any value as a result.
main:
This is the name of the method that we are going to use or the signature of the method. These are one of the vital attributes of a Java application because the main method is usually the starting point when running a Java program.
String[] args:
This is an array of Strings; It is a type of variable used in Java to combine a set of characters into a single string having a unique name. It is used for those things which have to do with the arguments that are provided to the program in the command-line mode.
Method Body
System:
This can be regarded as a predefined class level that grants the user the ability to interact with the system.
out:
This is the output stream that are connected with the console.
println:
This method makes a copy of the passed argument(s) and prints them out before ending the line of execution.
Detailed Explanation
Class Definition:
It is important that any Java program must have at the least one class definition. The class is more like the origin or the mold or the source of objects.
Main Method:
The main method is an entry point to any Java application I If you are developing your first Java application, then the main method is where your application starts. When a Java program is executed, the operation system invokes the main method of the class you have been instructed to create.
Printing to Console:
System. out. The last one, println, is used to print the output on the console or on the next line if it has been used before. System. out is an object of PrintStream and println is a method of PrintSteam class which prints a text at line.
Execution Flow
Compiling:
The Java source file (HelloWorld. java) contain a code which is compiled by Java compiler, which is known as javac which produced byte code file (HelloWorld. class).
When you run the program, JVM searches for the main method and beginning of the program execution starts with this method. They output “Hello, World!” to the console and then exit.
Conclusion
This first program is essentially a “Hello World” and in Java covers include class declaration, a principal method and output. It is vital to comprehend the aforementioned aspects if one is dedicated to advancing to the next level of Java programming.join our community for work this us
Tags:
Java