«Back to Home

Core Java

Topics

Simple Program Of Java

Program Of Java

Now, we will create a simple program of Java. First, we need to check, in which software, we need to write a program in Java.
  • Linux or Windows xp/7/8 operating system.

  • Java JDK 8

  • Notepad or any other text editor 
Let’s see, how to write a program in Java

In this program, first we create a class TestExample1 and print the statement.
 
Code is given below 
  1. public class TestExample1 {  
  2.   
  3.      public static void main(String args[]) {  
  4.            System.out.println("hello I am Java");  
  5.      }  
  6. }  
6
 
Write that program in Notepad first.
 
Save this file as a TestExample1.java.
 
Afterwards, open the command prompt Window.
 
Compile the program: javac TestExample1.java.
 
Run the program: java TestExample1.
 
Now, see the output

7
 
Explanation of program
  1. Class TestExample1{

    Class keyword is used to declare a class in Java such as TestExample1 is a class name.

  2. Public static void main(String args[]){

    • Public is an access modifier and is publically accessible everywhere means it’s visible to all.
    • Static is a keyword. When we use static keyword with any method, it is called as a static method. It is used basically in memory management and there is no need to create an object to call it a static method.
    • Void is a return type and it’s not returning any value to a method.
    • Main is used to start a program. Without main, you cannot run your program.
    • String[] args is used for a command line argument.
    • System.out.println() – it used to print a statement

      • System is a class name, which is inbuilt class.
      • Out is a static object of the class.
      • Println is a method to print a statement. 
One thing is important to know, how to set a path in Java. If we are not first, set a path. Hence, it gives an error that “javac is not recognized as an internal or external command”. Path is not required, if you save your file in jdk/bin directory and if you have not saved the file inside jdk/bin directory. Hence, it gives an error and you need to set a path to solve the problem.
 
Summary
 
Thus, we learnt, how we can write a program in Java with the explanation.