First Java Program

In this section, let's learn how to write a simple program in java. Write a simple hello java program after installing JDK . To create a simple java program, you need to create a mainclass that contains methods. Let's first understand the requirements.

Requirements for the Hello Java example

To execute any java program, you first need:

  • Install the JDK, if you don't have it, download the JDK and install it.
  • Set jdk/binthe path of the directory, refer to:
  • Create or write java programs
  • Compile and run java program

Create hello java example

Now let's create hello java the program, that is, create a Simple class with the following code:

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

Save the above code in a file: Simple.javaSimple.java Compile the code in the file:

javac Simple.java

 
The binary code (Simple.class) generated after execution of compilation:
 

java Simple

 

Output result:

Hello Java
 

Explanation of the first java program

Let's see what class, public, static, void, main, String [], System.out.println()mean.

  • class Keywords are used to declare a class in java.
  • public The keyword is an access modifier indicating visibility, which means visible to all.
  • static is a keyword, if a method is declared as static, it is called a static method. The core advantage of static methods is that they can be called directly without creating an object. mainMethods are executed by the JVM, so it doesn't need to create an object to call the mainmethod. So it saves memory.
  • void is the return type of the method, it means it does not return any value.
  • main Indicates program start (entry of execution).
  • String [] args Used for command line parameters, which will be learned later.
  • System.out.println() is the printout statement. We'll look System.out.printlnat the inner workings of statements later.

Write a simple java program in the editor (Notepad) and save it as a Simple.java file. To compile and run this program, you can go to Start Menu -> All Programs -> Accessories -> Open Command Prompt .

To compile and run the above program, first change to Simple.java the directory where the saved file is located. In this example the directory is F:\worksp\javabase. Enter this directory at the command prompt and enter step by step:

Simple.java Compile the code in the file:

javac Simple.java

 
The binary code (Simple.class) generated after execution of compilation:

java Simple

 
Output result:
Hello Java

 

How many ways are there to write Java programs?

There are many ways to write a java program. Modifications that can be made in a java program are as follows:

1) By changing the order of the modifiers, the method prototype does not change.

Let's look at mainthe simple code of the method again.

static public void main(String args[])

2) Java arrays can use post-type, pre-variable or post-variable.

Let's look at main the code for the different ways of writing the method.

public static void main(String[] args)

public static void main(String []args)

public static void main(String args[])

var-args

3) Provide support for the main method by passing 3 dots

Let us see the simple code used in the mainmethod, the usage var-args that we will learn in the new Java features chapter var-args.

public static void main(String... args)

4) The semicolon at the end of a class in java is optional.

Let's take a look at the simple code below.

class A{ static public void main(String... args){ System.out.println("hello java4");

}

};

 

valid java main method signature

public static void main(String[] args) public static void main(String []args) public static void main(String args[]) public static void main(String... args) static public void main(String[] args) public static final void main(String[] args) final public static void main(String[] args) final strictfp public static void main(String[] args)

 
invalid java main method signature
public void main(String[] args)  
static void main(String[] args)  
public void static main(String[] args)  
abstract public static void main(String[] args)
 

Solve error: "javac is not recognized as an internal or external command"

If the problem as shown in the figure below occurs, you need to set the path. Since DOS doesn't know javacor javacommand, we need to set it path. In this case, the path is not required if the program is saved in jdk/binthe folder. But it is better to set the path, after setting the path, you can use or command javacanywhere . For Java setup path, please refer to: Java JDK Installation and Configuration