Introduction

In this article we will take a look on

  • how to setup the environment to execute java programs
  • Once we setup the environment we will write our first java program and look at the execution process and steps that happend in the backend
  • we will also look at the rules and naming conventions that are to be followed while writing a program in Java

Environment setup


In order to execute the java programs we have to setup environment and need to install a java editor. We will go with downloading and configuring and the environment of java first and later with the java editor.

Environment setup


Java SE is freely available and you can download from the link Download Java. Choose a version from the list based on the operating system you have, if you have a 32 bit operating choose 32 bit version and same for the 64 bit.

Follow the instructions to download Java and run the setup.exe to install Java on your machine. Once you installed Java on your machine, you will need to set environment variables to point to correct installation directories

Setting Up the Path for Windows


Once you have installed Java in c:\Program Files\java\jdk directory −
environment-setup

  • Right-click on 'My Computer' and select 'Properties'
  • Click the 'Environment variables' button under the 'Advanced' tab.
  • Now, alter the 'Path' variable so that it also contains the path to the Java executable. Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'

Setting Up the Path for other Operating Systems


Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation, if you have trouble doing this.

Example, if you use bash as your shell, then you would add the following line to the end of your Java editors.bashrc: export PATH = /path/to/java:$PATH'.

To write a java program we need a java editor, there are also java IDE’s available in the market but for now prefer choosing one from the below list.

  • Notepad − On Windows machine, you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad.
  • Netbeans − A Java IDE that is open-source and free which can be downloaded from Netbeans
  • Eclipse − A Java IDE developed by the eclipse open-source community and can be downloaded from Eclipse

If you have a doubt on whether you have setup your environment correctly or not you can open your command line and type the command javac.

If you get any error message then it states that you have not setup the environment variables correctly. If you get the list of java binaries then you have successfully completed the environment setup for Java.

Writing first program in Java

Now let’s proceed with writing our first program in java. We will write a program to print name


public class name
{
	public static void main(String[] args)
	{
		System.out.println("My name is King");
	}
}
Follow these steps to execute the program you have written
  • Save the program with name Test.java
  • Open command prompt and point it to the directory where you have saved the file(Assuming it to be C:) C:\test>
  • Now type the command javac Test.java and press enter this will compile the java file and check if there are any errors present. If there are no errors it will take you to next step.
  • Now type the command java Test.java and press enter this will display the output.
Output :

							My name is King
							

Now let's have a look on what happend in the back end for execution.

Understanding the output process

Here are the steps that happened in the backend for getting the output of the code that we have written.
output-architecture
Source code


The java code that is written either in the editor or IDE is referred to as source code. There is a program structure on how this java code can be written we will refer to that in next coming articles. Here the file is Source Code

Compiler


The compiler checks the source code if there are any errors in the code it will not let you go to the next step, here we will check all the errors and the error free code will be moved to the next step. Here the file is converted in Byte code and stored as .Class

Output (code)


The compiler will convert the error free code in a java coded language known as Byte code. Any device capable of running java will be able to interpret/translate this file into something it can run.

The compiled byte code is platform independent and can be run on any device capable of running Java. Here it will be .Class

Virtual Machine


Everyone will not a physical java machine, they will have a virtual java machine. This virtual java machine will run the byte code and will get us the output. The JVM takes the .Class file and translates the byte code in to something the underlying program understands and displays the output.

Specification of JVM

In simple terms a Java Virtual Machine can be used to run Java applications and this also helps in the garbage collection process. The Java Runtime Environment (JRE) is also present inside the JVM. The main components of JVM include

Class loader


The main organisational units of the JVM byte code is class. Class loader must be designed in such a way that is has to recognize and load anything that conforms to Java class file. It is free to recognize other binary forms beside the class files, but it must recognize the class files. The class loader performs three basic activities,
  • Loading: finds and imports the binary data for a type
  • Linking: performs verification, preparation, and (optionally) resolution
  • Verification: ensures the correctness of the imported type
  • Preparation: allocates memory for class variables and initializing the memory to default values
  • Resolution: transforms symbolic references into direct references.
  • Initialization: invokes Java code so that it initializes class variables to their proper starting values.

In general, there are two types of class loader:

Bootstrap class loader

When JVM is started the bootstrap class loader is responsible to load key Java classes (from java.lang package) and other runtime classes to the memory first. The bootstrap class loader is a parent of all other class loaders. This is the only class loader without a parent Extension class loader

This will have the bootstrap class loader as the parent and is responsible for loading class from .jar files kept in the java.ext.dirs path–these are available regardless of the JVM’s class path.

System path class loader

This is an immediate child of the extension class loader. This loads the classes from the directories and also from the jar files specified by the CLASSPATH environment variable.

User defined class loader.

This class loader is required when you need your class loader to behave differently to a normal class loader. For example, you might want to transform the byte codes, or load them from a place that the standard implementations cannot cope with. At those times we can define our own class loader.

JVM Languages.


JVM language can be defined as a language with functionality that can be expressed in terms of a valid class file which can be hosted on a Java Virtual Machine. Class file contains instructions for JVM and a symbol table

Byte code verifier


The basic philosophy of java is that it has to be inherently safe from a user stand point and no user program can crash the host machine, and protect the inappropriate interference with other operations on a host machine.

It is also used to protect common programmer errors that often led to data corruption and unpredictable behaviour like accessing end of an array or using an uninitialized pointer, things like these are not allowed to occur.

The JVM verifies all byte code before it is executed. This verification consists primarily of three types of checks:

  • Branches are always to valid locations
  • Data is always initialized and references are always type-safe
  • Access to private or package private data and methods is rigidly controlled

Byte code interpreter and Just in time compiler

Each hardware architecture will have a different Java byte code interpreter. When a computer has a Byte code interpreter, it can run any Java byte code program, and the same program can be run on any computer such as an interpreter.

When a Java byte code is executed by an interpreter, the execution time will always be more when compared with the same program compiled in a native machine language. This problem is resolved by JIT (Just in time) Compiler.

This compiler translates the Java byte code into native machine language while executing the program. The translated parts can be executed more quickly and can be interpreted faster.

Rules to be followed while writing java program

  • Case Sensitivity − Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
  • Class Names − For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case. Example: class MyFirstJavaClass
  • Method Names − All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case. Example: public void myMethodName()
  • Program File Name − Name of the program file should exactly match the class name. When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name (if the file name and the class name do not match, your program will not compile).

    Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'
  • public static void main(String args[]) − Java program processing starts from the main() method which is a mandatory part of every Java program.

About Author

This is Krishna.I am the author of this blog. I am a technology enthusiast. If you found this article helpful please share it with your friends. Please provide your valuable comments on this blog.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions