How to Create Your First Java Program?
How to Create Your First Java Program?

Before we start writing our first Java program, let us see how does a Java program actually look like. See the code snippet given below. Don’t worry if you are not able to understand the terms written in the code, it’s completely fine. For now, just observe the structure of the Java code written below. It contains many keywords like class, public, static, void so on and so forth. We will discuss each and everything in detail.
Introduction
The Java programming language was created by James Gosling at Sun Microsystems in 1995. It is a high-level, general-purpose, object-oriented programming language. It is the most popular and widely used language. Java is based on the WORA rule. WORA means Write Once Run All. That basically means after compiling Java code, we can run it on any platform without recompiling it. So, in this post, we are going to discuss how we can write our first Java program. We will discuss different methods to write the hello world Java program. We will discuss each and every syntax and necessary semantics to write our first Java program. Statements that you just observed in the above code will start making sense after understanding all the included keywords. In the world of programming, it is necessary to understand what and why behind the code rather than actually writing it. Before proceeding further, let us see what are the prerequisites that are required to compile and execute a Java program.
Prerequisites:
* Java must be installed on your system.
* You should be able to compile and run a java file.
javac <filename[dot]java> command is used for the compilation of Java code and java <classname> command is used for executing the java class file. Both the commands are shown below.

Installing a Java compiler on a local system would definitely take some time, so in order to prevent it, you can use this Java online compiler. You don’t need to install anything on your local system, just write your code in the code editor and hit run.
Shortcut to the java online compiler: Type ide.new/java in your browser tab.
Let’s Understand the Structure of the Java Program
As we know, Java is an object-oriented programming language. Each time we write a Java program, we have to write at least one class. So, to write a class we’ll use the class keyword followed by the class name(as in our case it is FirstJavaProgram). Now, we also know that a class contains variables and methods associated with it. We can create any number of variables and methods inside a class. But, as we are writing our first Java program, we’ll write only one method i.e- main(). This is the entry point for JVM(Java Virtual Machine) to run our program. Now a question may arise in your mind: why only main() method, and not entry() or anything else? Because our JVM will always search for the main() method to execute the program, we cannot do any modification in the JVM. That’s why we write main( ). And to ensure that the main is visible and accessible from everywhere, we use public keyword. Remember, the name of your java program file must match with the name of the class that contains the main() method.
Why is the main method in Java always static?
Now, this is a 100 dollar question, let’s discuss it. First, let’s see what is the exact use of static keyword in Java. As we know that to use the methods and variables of a class, we need to first instantiate it (or create an object of that class). But if we declare any variable or method as static, then those variables and methods become class level members instead of object level. Now, we can access those static member variables and methods directly without instantiating our class. To access static members of the class, we simply need to use the class name [dot] member name, that's it.
Now come to the actual question: why is the main method always needed to be static? The reason behind it is that JVM (Java Virtual Machine) needs to call the main method to initiate the execution of our Java program, JVM is not going to create an object or instantiate the class; it simply searches for the main method in a public class by using class name only. If we suppose the main method is not static, then JVM needs to instantiate the class and for that, it needs to call the constructor (and the problem will become bigger when there are multiple constructors). You will learn about constructors in the further journey of Java learning. For now, if you are a beginner, consider that it is mandatory to declare the main method as static.
Why Main Method is Public and Void?
Public is a reserved keyword that specifies the accessibility of class, methods, and variables as public. It means any class, methods, or variables declared as public can be accessed from anywhere. Therefore, the main method is declared as public so that it can be visible or accessible to JVM so that JVM can execute a Java program. If we remove this public keyword, then JVM will give a runtime error as follows.

Every method is associated with a return type, the method can return any primitive data type, non-primitive data type, or even nothing. So, if methods don’t need to return anything then that method is declared as void. Void means nothing or empty. As we know, the main method is the entry point for a Java virtual machine to execute a Java program. After the execution program terminates, the work of JVM is also done. So, it does not make any sense to return something to JVM. What will the JVM do with that returned value? Useless.
What is String[] args - The Command-line Argument
When we execute our bytecode file (the file that was generated after successful compilation of our java code file, with the extension of .class) on the command line using command java <class name> then we can also pass arguments to the program. The arguments that the user passes, right from the command line that arguments are stored in this array of type string.

Output

Here args[] is the name of the String type array. It stores an array of strings (group of strings). Remember, this array can also store other types of data as well but in the form of strings. Further Java provides methods that convert these strings to respective data types. It is not mandatory to name it args only, programmers can take any name of their choice.
What happens if we do not write String args[] in the main method?
During compilation, no errors will come, it will compile successfully and a bytecode file will also be generated. But, as we discussed JVM searches for the proper main method to initiate the execution of the Java program. But, if we remove String args[] then the method signature will change and JVM will be unable to find it. Therefore, JVM will give a runtime error - “error: main method not found…”. Remember JVM always searches for the main method with the argument of string type array.
Println - My First Java Program
Inside the main method, we write our logic, which means what we want to do. As this is a demonstration of the first Java program, I simply wrote a print statement that will print “My First Java Program !!” on the output console.

In Java, we can not call println() method directly, because Java is an object-oriented programming language. So, we need to the object of that class from where the print method belongs to. This method belongs to the PrintStream class, so we should call the print method by using the object of the PrintStream class provided by Java. Now, here is a twist that PrintStream class does not allow to instantiate it, if we try to do so, it will give an error- “error: no suitable constructor found for PrintStream(no arguments)”. As a solution, an alternative way is given to us, i.e., System.out. Here System is a class present in java.lang package and out is a static variable of the class System, that stores the object of PrintStream class. As we discussed above, a static member of the class can be accessed through the class name itself. Therefore, we wrote System.out which gives an object, and then called println method (combined written as System.out.println). Remember, the PrintStream object represents a standard output device, i.e. Computer’s monitor.
Now if you see the below code once again, each and every word would make sense to you. Now you know the reason behind why we have made a class, why main() method is public, static and void so on and so forth.

Now click here and code your first Java program.
Conclusion
In the article, we have taken our first step toward writing a Java program and we understood each and every syntax and semantics of our first java program. We have discussed the initial concepts of object-oriented programming by writing a class with a main method. Along with this, we got familiar with the main method, the reason behind why we declared it static, public, and void. Further, we discussed command-line arguments and the concept behind System.out.println statement. Now, it is recommended that to write your first Java program and try to modify the correct code by deleting some keywords or adding different keywords, then run it and observe which type of errors it’s giving. All the best !!



Comments
There are no comments for this story
Be the first to respond and start the conversation.