Loading
Java-First-Program

In this chapter, we’ll write our first Java program and understand the role of the main() method.


First Java Program

package quipohouse; public class FirstProgram { public static void main(String[] args) { System.out.println("Welcome to Quipo House"); } }


Output:

Welcome to Quipo House

Understanding The main() Method

The main() method is the entry point of any Java program. Here’s why it’s important:

  •   Predefined Method: The JVM starts executing the program from the main() method.
  •   Overloading Possible: You can define multiple main() methods with different parameters.
  •   Public Access Modifier: Must be public so that the JVM can access and execute it.
  •   Static Keyword: Allows execution without creating an object, improving memory management.
  •   Void Return Type: Does not return any value after execution.
  •   String[] args: Accepts command-line arguments as an array of strings.

In short, the main() method guides the JVM to start program execution and allows user inputs via command-line arguments.



Explanation of main method declaration:





Explanation of print statement:

The System.out.println(); is used to print the output in the console.





How to Create & Run a Java Program

Follow the steps given below to write and execute your first Java program:

Step 1: Write the Program

  • Open Notepad or an IDE like IntelliJ, Eclipse, or VS Code. (Image 1)
  • Type the Java program and save it with a .java extension. (Image 2 to Image 4)

  • Image 1


    Image 2


    Image 3


    Image 4

    Save the file with Class Name and with Java extension.

    Select file type (All Files).


    Step 2: Compile & Run the Program

    • Open Command Prompt (Win + R, type cmd, and hit Enter). (Image 1)
    • Navigate to the folder where the file is saved using cd Folder Path.
    (Image 2 to Image 7)


    Image 1




    Image 2


    Now copy the path of the folder where you create the Java file


    Image 3


    Type the command cd and hit enter.


    Image 4


    Now type cd with path  (i.e. cd documents than subfolder java).


    Image 5


    The class file of the java file is now displayed in the folder where you saved your java file.


    Image 6


    Now type javac and Java file with the extension


    Image 7


    Now just type the java and the file name and then hit enter.

    You have successfully executed your first Java program.


     What happens if there's no main() method in Java Program

     

    If we try to run a Java program without a main() method, the JVM won't know where to start execution, resulting in an error. as shown below

    public class MyClass {
      // This is not a main method
      public static void myMethod() {
        System.out.println("Welcome to Quipo Tutorials!");
      }
    }


    After running the code by using the command java we will get the following output:


     

    Two Minute Drill


    • Creating a New File: First of all, open IDE and create a new project, and add a new Java class file. For example, HelloWorld.java.
    • Write the Code: Add the basic code structure with the main method:
      public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }

    • Compilation of Program: Save and compile the code using any integrated environment or from the command prompt using the command javac HelloWorld.java.

    • Run the Program: Run the program to show the output, "Hello, World!", and by doing so, make sure everything is running correctly.