Moving from C++ to Java (J4A – 1)

C to C++ and C++ to Java:

C was a powerful programming language but used structural approach. So when the length of code exceeded a particular no. of lines, the code became unmanageable. So this led to Object Oriented Programming techniques and birth of C++. But C++ compilers at that time were not platform independent. You needed a different code for Windows, different for Mac, Unix and other operating systems. Today programs written in C++ are also categorized by 32 and 64 bit versions. So C++ followed the W.O.C.A. principle, i.e. Write Once, Compile Anywhere. The main focus was to create programs which could execute on any platform and any O.S. These forces led to the development of Java (originally called as ‘Oak’) which followed W.O.R.A. principle, i.e. Write Once, Run Anywhere.

 

Where the Magic lies?

So what magic is there in Java which was not in older languages? Basically all the older compilers compile the source code (program) to native executable file (like .exe in windows). Here ‘native executable file’ means a file which is created for some target OS and is ready to execute on that OS. So we need to create a different ‘native’ file for different OS and different processor architectures (32 and 64 bit).

But in Java, the code is not directly compiled to native executable. The output of java compiler is a .class file, irrespective of OS / processor architecture. This .class file is a set of optimized instruction for a virtual machine called JVM (Java Virtual Machine). The creators of Java implemented this virtual machine (JVM) for different operating systems. So now the programmers need not worry about the target operating system or processor. They just have to write the code and install JVM on the target system, JVM handles the rest.

 

Understanding Simple Java Terms:

JVM is simply a virtual machine included in JDK as well as JRE.

JDK is Java Development Kit for Java Developers. It includes Java Runtime Environment(JRE) and all other related tools needed by developers.

JRE is Java Runtime Environment. It excludes the development tools and encapsulates only runtime environment. When you create a java application for a client, just install JRE on client side and your application will execute.

Remember, Java code is platform independent but Java Runtime Environment is specific for each platform.

The Android Counterpart: Android Devices have DVM (Dalvik Virtual Machine) rather than JVM (Java Virtual Machine). You can think of Dalvik as mobile or android specific implementation of JVM. You need not know about Dalvik for now, for those who are interested can visit here.

Downloading & Installing JDK

Download JDK from here.

1

Scroll down to see screen as shown above. Accept the License Agreement to start download. As you can see, JDK is implemented for almost all major operating systems. After downloading, install JDK with default settings. After installation, go to

C:/program files/java/jdk<version_name>/bin/

Make sure ‘javac’ and ‘java’ are present in this folder. Now copy the complete path from the address bar of explorer as shown in the pic below.

2

 

Now, follow these steps

Go to Start Menu -> Run. Type ‘sysdm.cpl’ and press OK. You get the following screen.

 

3  Click on ‘Advanced’ tab and then on Environment Variables button.

4 You will get following screen now

5
Create a new user variable using button as shown above. Write the following values in text field provided.

Variable name : path

Variable value : <text you had copied>.

Press OK to save and close all previous dialog boxes.

Now your Java Environment Variable is set and if you have followed the steps correctly, you will be able to compile and run java applications. To check if you have followed the above procedure correctly, goto start menu -> Run and type

java and press enter, if you get a flashing black screen, type

javac and press enter, if you still get a flashing black screen, it means the variables are correctly setup. If you get some error when you type java or javac, you might have done some mistake in setting up variable. It’s better to restart your computer and try again.

The next tutorial describes creating your first simple Java program.

Link to next tutorial

3 Comments

  1. Nicely written…
    Would you please explain how to configure JDK on linux.

    • Install JDK from the given link, it is available for all major operating systems and processor architectures.
      After installing JDK, you just need to setup the environment variable, which can be done using set command in linux as shown below

      export PATH=$PATH:/usr/java/jdk1.5.0_07/bin

Leave a Reply

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