Loading

First Program And the main() method

  • In this chapter, we will Write our first Java program and understand the basic structure of the main method.

Example:

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

Output:-

Welcome TO Quipo House

main() Method

  • The main() method is a predefined method in Java. The JVM starts compiling the program from the main method.
  • We can Overload the main method also.
  • It always be public because JVM needs to access the file for compiling the program.
  • The keyword static is used because of memory management and JVM does not need to create an object to execute the main method.
  •  The return type of the main method is void because it doesn't return any value or data after execution.
  • The String [ ] args is used to pass the input into the main method as arguments or parameters. " String [ ] " is called a string array means a group of arrays. Users can insert a string array as input through the command line.

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 and execute a Java program

Step-0: Open the notepad.


Step 1: Write the program.


Step 2: Click on the file then click on save.


Step-3: Save the file with Class Name and with Java extension.

              Select file type (All Files).


Step-4: Now copy the path of the folder where you create the Java file


Step-5: Open Command Prompt or Just Press WIN+ R It opens run and then type cmd and hit enter.

               The Command Prompt will pop up.


Step-6: Type the command cd and hit enter.


Step-7: Now type cd with path  (i.e. cd documents than subfolder java).


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


Step-9: Now type javac and Java file with the extension


Step-10: Now just type the java and the file name and then hit enter.

You have successfully executed your first Java program.


 

What will happen if we try to run a Java program without a main method?

 

For example, try to write this program and run it just like the above procedure:

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-min Drills
  • 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.