Simple Program of Java

Simple Program of Java

 

In this section we will demonstrate a simple program written in java programming language. As we have installed the JDK, now we can write the program in Java easily.

A simple program in Java can be created by creating a simple class first after the installation of JDK.

 

Requirement for Hello Java Example:

To run a Java program on your system you need to install the Java JDK, here is the link of Java JDK: Download JDK. Then you need to set the path for the JDK bin directory. Now create the Java program. You can compile the Java program and can also run it now.

 

Creating Hello Java Example:

Consider the following example in which we have created our first Java program:

CODE:

     class MyFirstProgram{

     public static void main (String args []){

     System.out.println(“Hello Java”);

      }

    }

    OUTPUT:

    Hello Java

hello-java

     Write this code on the text editor which has already been installed in your computer and save the file with the .java extension as done with the following editor:

my-first-program

 

Understanding first Java program:

In this section we will demonstrate the terms that are used in the above program. Consider the following points:

class:

class is a keyword which is used to declare a class in programming languages.

public:

public is a keyword in Java that is referred to as the access modifier. The keyword “public” represents the concept “visibility”. That is the variables are visible to which class or methods etc. The keyword “public” makes a variable visible to all.

static:

static is a keyword in Java that is used to declare any function or method as static. When we declare a method or a function as static then there is no need of creating other objects and invoking the functions or methods. In the java program the main method or function is executed by JVM. As the main method is executed by JVM therefore, we do not have to create objects that will invoke the main method. In this way we can save a lot of memory.

Void:

Void in Java programming language is a keyword that is used as the return data type of a function or a method. In general void means nothing; therefore the void data type does not return any value.

Main:

Main is the main function of the program in which all the methods of the program are called and in which the objects are defined for the calling of the functions that are defined in a class. The main function is used to represent the start of the program.

String args[]:

String args[] is considered as an array of the console line argument and the data type of this console line argument is string. We can store a number of string arguments in this array. This can be done by invoking them in the command line.

System.out.println:

System.out.println is the print statement in Java programming language. It is used to print on the output screen.

 

How to write a Java program?

A simple Java program can be written by using the note pad editor that is already installed in the computer system. Open the note pad by clicking on the start menu and then selecting all programs, click on the option accessories and select notepad from there. As you have opened the note pad now write the Java program in it.

Now save the program that you have written in the notepad with the name you want but the extension should be .java.

To run the program click on the start button and select all programs. Now select accessories and choose command prompt from the drop down menu.

Now choose the current directory in which you are working and to compile the java program write javac asd.java and to execute this java program simply write java asd.

 

How many ways can we write a Java program?

There are a number of ways in which we can write java programs. Some of ways through which we can write Java programs are given below:

 

By changing sequence of modifiers, method prototype is not changed:

If we change the sequence of the modifiers in a program then the prototype of the method is not changed. Consider the following code:

CODE:

static public void main (String args [])

 

Subscript notation in Java:

We can use the subscript notation in java array before or even after the variable. Consider the following codes:

CODES:

public static void main (String args [])

public static void main (String [] args)

public static void main (String [] args)

In the above example it can be seen that we have used the subscript notation for array after the type, before the variable and after the variable. This won’t affect our code and will compile and execute successfully.

 

Support to var-args:

The main method or function in Java can be supported by passing three dots also called three ellipses. Consider the following code in which we have provided var-args support to the main method or main function of Java program by passing three ellipses.

CODE:

public static void main (String… args)

 

Semicolon:

In Java programming language it is optional to put a semi colon after the definition of the class. Consider the following code in which we have put a semi colon after the definition of the class:

CODE:

     class MyFirstProgram{

     public static void main (String args []){

     System.out.println(“Hello Java”);

         }

    };

This will not affect the compilation and execution of the Java program.

 

Valid java main method signature:

Consider the following signatures that are valid in Java programming:

CODE:

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 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)

All of the above codes will be valid in Java programming.

 

Invalid Java main method signature:

Consider the following codes or signatures that are not valid in java and the program will not compile and execute because of them:

CODES:

public void main (string [] args)

static void main (string [] args)

public void static main (string [] args)

abstract public static void main (string [] args)

 

Resolving the error “javac is not recognized as an internal or external command”:

If there occurs a problem that javac is not recognized as an internal or external command then the user should set up the path this is because the command prompt does not know about javac and java. This is the reason why we are setting path so that the command prompt knows what javac and java is. If the Java program is saved inside the jdk\bin folder then we do not have to set the path to compile and execute our program. But to set path is a good practice to avoid errors.