The main()
method in Java is the entry point for any Java application. It is executed by the Java Virtual Machine (JVM) when the program is run.
Let us analyze the syntax in detail and examine the purpose of each individual component.
public: public
is an access modifier. Access modifiers in Java specify the accessibility or scope of a method. The access level of a public
modifier is everywhere. It can be accessed from within the class, outside the class, within the package, and outside the package. We make the main()
_ method _public
so the JVM can access the method from anywhere.
static: The static
_ keyword means the _main()
_ method belongs to the class itself, not to any object. So, you don’t need to create an object of the class to run the _main()
method.
void: void
means the main()
method doesn’t return any value. It's just used to start the program, so it doesn’t need to send anything back. In Java, every method must mention what it returns, and void
means 'nothing'.
main: main
is the name of the method. It must be written exactly as main
, because the Java Virtual Machine (JVM) looks for this method to start running the program.
String[] args: _String[] args
_ is a way to receive input from the command line when the program starts. It’s a list (or array) of text values that you can use in your program if needed.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Use our Online Code Editor
In this program, we are simply printing the data on console.
Output
Hello, World!
Use our Online Code Editor
How to take Command Line Arguments?
public class MasteringBackend {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("First argument: " + args[0]);
} else {
System.out.println("No arguments passed.");
}
}
}
Use our Online Code Editor
In this example, we are taking inputs from the command line by passing the parameter at runtime using the command below.
java MasteringBackend Hello
Use our Online Code Editor
As we passed the first argument as Hello
, we get the output as shown below.
Output
Hello
Use our Online Code Editor
Using String… args
String[] args
and String... args
are both the same.
public class MasteringBackend {
public static void main(String... args) {
for (String arg : args) {
System.out.println("Argument: " + arg);
}
}
}
Use our Online Code Editor
In this example, we are taking input from the command-line arguments. When we run the command by passing all the numbers of parameters, all the values are printed to the console.
java MasteringBcakend Learn Java Backend
Use our Online Code Editor
Output
Learn Java Backend
Use our Online Code Editor
Note
- The _
main()
_ method is declared aspublic
so that the JVM can access it from outside the class. It is marked asstatic
so it can run without creating an object of the class. If it is notpublic
, the program will not start.
System.out.println()
System.out.println()
is a statement that is used to print data to the console.
System:
System
_ is a special built-in class in Java that allows interaction with the underlying system, such as printing messages to the screen or reading input from the keyboard. It's part of Java's core library _(java.lang
package).
out: out
is an object that is part of the System
class. It acts as a channel for sending data to the console. When you want to display information to the user, you send it through `System.out.`
println(): _println()
_ is a method that prints the specified text to the console and moves the cursor to a new line. This makes it easy to display each piece of information on a separate line.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Use our Online Code Editor
In this example, we are printing the data to the console.
Output
Hello, world!
Have a great one!!!
Author: Solomon Eseme
Thank you for being a part of the community
Before you go:
Whenever you’re ready
There are 4 ways we can help you become a great backend engineer:
- The MB Platform: Join thousands of backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.
- The MB Academy: The “MB Academy” is a 6-month intensive Advanced Backend Engineering Boot Camp to produce great backend engineers.
- Join Backend Weekly: If you like posts like this, you will absolutely enjoy our exclusive weekly newsletter, sharing exclusive backend engineering resources to help you become a great Backend Engineer.
- Get Backend Jobs: Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board.
Top comments (0)