All Courses
Java Basic Programs

Java Basic Programs

May 2nd, 2019 Java

A sample java program :

In this blog post, you will acquire the understanding of creating a simple Java program. There are a few things that you will need for successful compilation and execution of Java code. You’ll learn step by step writing, compilation and execution of Java code.
Before you write your first program in Java and compile it, you’ll need a text editor to write the code and a software called JDK to compile and execute the code.

To write the code:

  • A text editor
  • An IDE

To compile and run the code:

  • Download and Install JDK i.e. Java Development Kit
  • Set the path

 Setting the path:

The path setting is mandatory for using tools like java compiler i.e. javac and java etc.
While setting the path keep following things in mind:

  1. If source code file is saved inside bin directory of JDK, path need not be set since all the tools exist in the present director.
  2. If source code file is saved outside bin, path setting becomes mandatory.

Types of path setting in Java:

Temporary path setting:

Temporary path setting is very easy. Follow the steps given below to set path temporarily:
Step 1: Go to command prompt and open it.
Step 2: Copy the JDK/bin directory path. Let’s say it is path_copied.
Step 3: Write the following command in command prompt:

SET PATH= path_copied

Permanent path setting:

Permanent path setting is a bit different from the temporary path setting. Follow the steps given below, for Window:
Step 1: Right click on MyComputer.
Step 2: Go to properties.
Step 3: Go to the advanced tab.
Step 4: Go to environment variables.
Step 5: Go to the new tab of user variables.
Step 6: In the variable name, write the path.
Step 7: In variable value, write the path of the bin folder.
Step 8: Click ok.

Writing a simple Java Program:

Everything is said to be an object in Java. It also means that everything has to be inside a class in Java. Even the main function, which is considered entry point of a program, has to be inside a class, in Java. Let us write our first program in Java using Notepad or Notepad++.
Once you have opened the text editor, you can easily write the following code:

class first_program
{
   public static void main(String args[]))
       {           System.out.println(“A simple program in Java”);
        }
}
Note:

  • Save this program file as JavaSimpleProgram.java.
  • When you save the file, make sure that file type is selected as “all files”.
  • Save the file in the working folder only.

Compiling and executing the program:

  • Go to command prompt and open it.
  • To compile the code, type ‘javac JavaSimpleProgram.java’.
  • This will create a file called JavaSimpleProgram.class.
  • To run the code, type ‘java JavaSimpleProgram’.
  • It will display ‘A simple program in Java’ as an output message on the screen.

Using an IDE:

If you are using an IDE viz. NetBeans or Eclipse, you can copy paste the code in the editor of IDE and use debug, compile and run buttons there to execute the code. Use of IDE improves your efficiency and reduces the time taken to run the code.

Understanding the simple program in Java:

Java programming language is case sensitive. It means that lower cases are different from upper cases. It means ‘JAVA’ is not the same as ‘java’.
In the program example here, different parameters have been used. Let us understand what they are and why they are used in the program.

  • Class: This is the very first keyword used in the program. It is used to declare a class in Java.
  • first_program: This is an identifier. That means this name is given by the user. It could be anything. It is used to refer to a class in the program.
  • public: Another keyword used in the program, ‘public’ is an access modifier. It is used to represent visibility in the code. Different access modifiers are ‘private’, ‘public’, ‘protected’. ‘public’ means it is detectable to all.

Note: For the beginners, Notepad or Notepad++ can be used.

import java.io.*;
public class Basic {
public static void main(String[] args)
{
System.out.println("Java World invites you!");
}
}
This program is saved as Basic.java, where Basic is the file name and .java is the extension. Java filename should match with the class name and classname must start with the uppercase letter.
In this program, apart from red and blue coloured words, remaining words are keywords.

Now, what is a keyword ?

A word which has a predefined meaning to be understood by that particular device (here, our computer) & to perform an action based on the instruction given in the form of that word, is called a Keyword.
The keywords used in this program are :

import :

import is used just like ‘#include’ in ‘c’. It is used to provide the accessibility of  java packages which are mentioned near this keyword.

Example: import java.io.*;

In this ‘java’ is a package & ‘io’ (which is input output) is a sub package. And ‘*’ denotes all the classes from java’s io package.

Now, what are packages and classes ?

Classes: To be short, classes are just like a template which hold certain properties and behaviours. (In detail, will be dealt under Classes and Objects topic).
Packages: Packages are just folders used to hold certain number of classes.

Now, ‘import java.io.*;’ says to include all the features available in classes of java’s io package.

  • public : is an access specifier. This keyword indicates that this class may available to other classes also if they want to use its features.
  • class : Classes are just like a template which hold certain properties and behaviours.

Here, in Class Basic, Basic is the name of the class.
Now, public static void main(String[] args)

  • static : In java, to call every properties and behaviour, objects are used. Here, the static is given for main method. For calling main method, we can’t use object. When static keyword is used for methods, the method is called automatically during runtime.
  • void : Since, main method does not return any value, void is used as return type.
  • main(String[] args) : ‘main’ is the name of the main method. String[] args is a String array, ‘args’ is the name of String array.
  • System : System is a class that indicates our system.
  • out: is an object used for output.
  • println() : is a method called by out object. The content given inside the brackets of this method will be printed on console.
  • In this program, “Java World invites you!” is a string which will be displayed on console.

How java runs ?

Ans: For java, we install jdk (Java Development Kit). Jdk contains jre(Java Runtime Environment). Within jre, there exists jvm(Java Virtual Machine).
This jvm compiles the code of Basic.java file, i.e, it checks for any syntax error then converts Basic.java file to Basic.class file. This .class file contains byte code of our program, which is then read and executed by the cpu.

Social Share:

0 responses on "Java Basic Programs"

Leave a Message

Your email address will not be published. Required fields are marked *