Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Getting Started with Java

Java Backend Development: Zero to Hero

Getting Started with Java

main() Method and System.out.println() in Java

main() Method and System.out.println() in Java

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.

image (27).pngLet 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:

Helloworld.java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

In this program, we are simply printing the data on console.

Output

Hello, World!

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.");
        }
    }
}

In this example, we are taking inputs from the command line by passing the parameter at runtime using the command below.

java MasteringBackend Hello

As we passed the first argument as Hello, we get the output as shown below.

Output

Hello

Using String… args

String[] args and String... args are both the same.

public class MasteringBcakend {
    public static void main(String... args) {
        for (String arg : args) {
            System.out.println("Argument: " + arg);
        }
    }
}

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

Output

Learn Java Backend

Note

  • The main() method is declared as public so that the JVM can access it from outside the class. It is marked as static so it can run without creating an object of the class. If it is not public, the program will not start.

System.out.println()

System.out.println() is a statement that is used to print data to the console.

image (28).pngSystem: 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!");
    }
}

In this example, we are printing the data to the console.

Output

Hello, world!

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ 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 BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post 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