Have you ever tried “playing” the main() function in Java?

Tram Ho

Hello brothers!

A Java program is a sequence of instructions that are executed in a certain order. It is because in a certain order that the Java program will have a beginning and an end.

To execute a Java program, it is necessary to mark the Java virtual machine (JVM – Java Virtual Machine) where to start program execution. In Java, all statements, instructions, and code must be placed inside classes.

In general, the class will cover everything, the class is a way to encapsulate data and methods together. Thus a class can include both variables and methods. A variable can consist of data (data) and a method is a set of operations (operations) on that data.

In today’s article, I will “play” with you guys on the main() method in Java!

Okay, let’s get started!

Declare a Java Class.

First let’s see how a class in Java is declared. A normal Java class, without variables, without methods will be declared as follows:

Note that the name of the class must match the file name. In this case, my class name is HelloJava, the corresponding file name must be HelloJava.java.

This is the rule for the Java compiler to compile the program.

It is recommended to place Java classes inside Java packages. Essentially Java packages are just folders on the computer that group Java files together.

Note 1: When the .java file is in a certain package, in that .java file you must mark the package containing that .java file as shown above.

main() method

Just like C/C++ starts executing program in main() function, so does Java. A Java program needs a main() function to launch.

We can choose the class name to execute and then main() will always be called first. The main() method in Java is placed inside a class and declared with the required syntax as shown below. The only thing you can change is the parameter name passed in!

(1) – Are mandatory keywords public, static, void . You will not be able to rename, change the style or remove these keywords if you want to define it as the main() function.

public : The access modifier is public so that the Java compiler can call the function anywhere in the application.

static : When the Java application is launched, no object has been created. The main function will run first, so it needs to be static so that the program can call the main function when no object has been initialized.

void : The main() function will not return anything because when the main() function ends, the Java program ends. If you intentionally return it, you will get an error error: incompatible types: unexpected return value

(2) – As the function name, you are not allowed to change the main() function name. Must be “main”, intentionally changing will also error.

(3) – The main() function only contains a single parameter that is an array of String. These are called command line parameters. We can rename the passed parameters, for example: String[] arguments, String[] stringArray, String[] params… As long as it’s an array of String.

Run main() function in Java.

If using IDEs like Netbeans, Eclipse, InteliJ Idea, running a program with main() function is very simple. You just need to click the corresponding compile buttons of each IDE and you’re done (so many of you don’t know how Java programs are compiled, but is it necessary to know? No one notices when going to work! What do you think about it??)

In this article, I will join you to run a Java program containing the main() function with the command (command line). This is a simple HelloWorld program, I will print out the values ​​in the String array passed in by the command line.

  • Run the command javac <filename>.java to compile the .java file into a .class . file
  • Continue running command: java <filename> 

This is what it means and how you pass parameters to the main function. Thereby helping us better understand how the main() function in Java works. Since it is a command line parameter, you can only pass it from the command line. This is the execution flowchart.

Note 2 : A Java class can have only one main() function. But each Java class in the application can theoretically contain one main() function, ie the application can have many main functions in different classes. But we cannot call multiple main functions at the same time, only one main() function calls another main() function and these functions will be executed in order.

Conclusion:

So in this article, I went with you into the details of the structure as well as the rules of the main() function in Java. Although it is just a very simple knowledge, I think not all of you notice it. I hope the article will help you somewhat better understand how the main() function in particular and how a Java program is executed and compiled. If the article is missing, please comment. Thanks all!

Share the news now

Source : Viblo