SIMPLE JAVA PROGRAM

As we've set up the environment in our system so far, let's start with a simple example. We'll learn the basic syntax of a simple java program in this module.

Table of Contents

[hide]
  • Requirements to Run a Program
  • The Creation of Simple Java Program
  • Compile and Run the Program
  • Important KeyTerms in the Program

Requirements to Run a Program

For executing any Java Program we will require following softwares :

The Java SE Development Kit

If you've been following our tutorial from the start, we've explained the setup of the Java environment in detail. If you have not installed then kindly go to this link and install. LINK : Download Java SE Development Kit(JDK)

A Text Editor

In this example we will use NOTEPAD as it is a simple editor included with Windows Operating System. You can use any of the text editors or softwares like Visual Studio Code, Eclipse, NotePad++ etc.

The Creation of Simple Java Program

To start writing your first Java program please follow the following steps.

STEP 1 : Open NOTEPAD by clicking on Start menu -> Programs -> Accessories -> NotePad.

STEP 2 : Start writing your first Java Program.

copyimage
// A Simple Java Program to demonstrate the syntax.
public class JavaLearning{
  public static void main(String args[]){  
      System.out.println("Welcome to Java World");
    }
  }

No need to worry if you don't understand the program, it's just to learn the basic syntax. Highlighted KeyTerms are explained in the later section of this module.

Compile and Run the Program

Now that you've written your first program, it's time to check the output. The following program will be compiled and executed with the following steps.

STEP 1 : Save the file with name JavaLearning.java. If you haven't set the java path yet, save the file in the jdk bin directory otherwise you can save it anywhere. To set path in Java go to our learning module LINK : Set path in java.

NOTE :File name in Java should be same as public class name. It is a way to tell JVM that this is an entry point. Suppose if we construct a program in which there is more than one class file, then interpreting which class contains the program entry point will be ambiguous for Interpreter and JIT compiler. In our case file name is JavaLearning.java.

STEP 2 : Open command prompt, go to your working directory where your JavaLearning.java file resides. Compile the code using command.

Javac JavaLearning.java

STEP 3 : Now if you look at your working directory you will see that JavaLearning.class file has been created. A Java compiler takes the JavaLearning.java source file and compiles it into a platform-independent JavaLearning.class file called bytecode.

STEP 4 :Now to execute the code write the following command

Java JavaLearning

Java is case sensitive programming language. JavaLearning is not same as javaLearning

You will finally receive the output of your First Java programme. Congrats !!

OUTPUT : Welcome to Java World

Important KeyTerms in the Program

The basic definition of keywords that we used in our previous program are given below. We will discuss about internal working of each of them later in the upcoming modules.

Java class

Java class is a blueprint of an object. It describes the properties and methods of an object.

public keyword

public is a keyword in java which means any other class can access a public field or method and public members are visible to all other classes.

static keyword

static is a keyword that states that JVM can access the main function without creating ab object of a class. It belongs to the class than instance of a class.

void

void is a java keyword used at method declaration that states that method does not return any type (value).

main method

It is the starting point of java program that JVM looks for. You have to have a main method in at least one class.

String args[ ]

String args[ ] in Java is a string array that stores arguments passed by a command line while starting a program. All arguments for the command line are stored in that array.

System.out.println()

System is a final class defined in the java.lang package,out is an instance of PrintStream type,println() is a method of printStream class.