Java is a high level, English like, object-oriented programming language with tons of APIS
(application programming interface) for developing applications. Java is a platform independent language with support for performing complex computation, graphics, server and mobile application development.
Java language syntax looks pretty much like C and C++, however there are some differences such as absence of pointers, allows writing object-oriented code. Learning Java is easy for a developer with basic C programming experience.
Java is a platform independent language
Java is one of the vastly used languages as it supports “write once run anywhere” architecture. A java program written in Macintosh can be executed on Windows or Linux without any modification or without recompiling it again. To understand how Java achieves platform independence, we must understand the way java program is compiled and executed.
Writing first Java program
A simple java program can be written in any text editor such as notepad, WordPad, notepad++ or edit plus. While writing your Java program make sure it must have one public class and one public static void main()
method which allow it to run as java program when invoked in Java Virtual Machine.
For now without worrying much about how to go with it, just copy and paste the following code in your favorites text editor. We will learn about the building blocks in details on the following sections. After pasting the following code, save your file with name HelloWorld.java extension. Now we are ready for next step to compiling and run Java program.
/* Hello World Java Program */ public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello World!”); } }
Compiling Java code
Compiling java code requires a java compiler that comes along with Java Development Kit also called as JDK. This tutorial assumes that you have already installed Java JDK on your development computer, if not you can download and install from Oracle’s site.
If you are not sure, if java is installed properly, you can check by entering javac –help
command in your command line. If installed properly, you will see the output as depicted in the screenshot below.
Once you are ready with your java development environment, you can compile your java code by invoking javac
command from command line. Enter the following command to compile HelloWorld.java
> javac HelloWorld.java
The above code will compile your java source file and produce a new file named HelloWorld.class
. Open HelloWorld.class file in a text editor; notice that it contains some unreadable byte code. This Class file is actually java’s way to achieve platform independence. This class file contains JVM specific byte code instead of machine code unlike the C or C++ that makes it possible to run java program in any platform.
Running Java application
We know that after compilation java compiler creates .class
file that contains JVM specific byte codes and to execute those byte code we need a piece of software called Java Virtual Machine or JVM. JVM is also known as Java Runtime or JRE. Byte codes of java programs executed inside Java Virtual Machine which is platform dependence, you here it correctly JVM are indeed platform dependent and it has different binary for different platform which you can down load from Sun’s site. JVM also comes with JDK or you can download it separately.
Use the following command to compile the above code
> java HelloWorld
Output of the above java program is
Hello World!
Importance of Java Virtual Machine
It’s important to know that JVM or Java Virtual machine doesn’t have any information high-level java code. It knows only binary format of the byte code and ensures that the class file generated by java compiler is as per Java Byte Code specification thus eliminating dangers of running a malicious and manipulated java byte codes.
Since every Java program runs within the boundaries defined by Java Virtual machine or JVM it provides inherent Security. The code run inside the JVM cannot go beyond the security constraints defined by the Java Virtual Machine. This is why java application are considered as secure applications over internet and Java applets are most widely used programming platform for internet. Apart from Security Java Virtual machine also provides memory management via Garbage collection that enables java programmer or java developer to focus more on business logic rather than worrying about system aspects like allocating and reclaiming memory.
In conclusion with the use of class file, byte code and Java virtual machine, Java achieves its platform independence and makes development easy to run across different platform.
