Writing First Program in java

There four steps to designing our first java program.
Step 1:- Select an Editor.
Step2:- Write a Program & save the program.
Step3:- Compile the program.
Step4:- Execute the program.


Step1) Select an Editor

An editor is a tool or software it will provide a very good environment to type the java application.
There many editors available for example-

  Notepad, Notepad++ ,Sublime etc.

  We can also use IDE's for compiling and Executing our java program.
For example
Netbeans, Eclipse , IntelliJ etc.

Step 2) Write the program and save the program

Write the java program based on the Java API(Application Programming Interface) rule and regulations.
Java is a case Sensitive Language while writing the program we have to take care of the cases(Upper or Lowercases) (Alphabet symbols)
After writing the program we have to save the program.
while saving the program we have to consider following steps-
Check whether the source file contains the public class or not

  1. if the source file contain public class, the name of the public class and the name of the Source file must be same(publicClassName.java). Otherwise, we will get compile time error.

  2. if the source file does not contain any public class at that situation we can save the source file with (anyName.java).

Step3) Compiling our java program

To compile single source file :


C:> javac FileName.java      :Press Enter
//for example 
C:>javac Test.java
To compile the multiple file in current directory:
use the cammond:

C:>javac *.java      :Press Enter


  The java compiler goes to Test.java file and it will search for syntactical error if the syntactical errors are found then java compiler raise compilation error if there is no syntactical errors are found in that situation the java compiler converts the .java files into the .class file.

  Note: The .class file generation totally based on the number of classes present in the source file.

Step4) Executing the program

For executing the java program use the command:

C:>java ClassName   :Press Enter

  Whenever we type java classsName in the command prompt first it will search for the className if the class is available then it will search for the Main method if the main method is there the program execution starts from main method onwards.

Let us write our first java program .

Open any text text editor.


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

How to compile?

  Open the command prompt.

  Go to the directory where you saved the .java file.

Now execute the command : javac FileName.java

For this program : java First.java

How to execute ?

Execute command: java ClassName

For this program: java First

Output

Hello Java

woman avatar