You remain well-grounded when you know your roots. Same is true about Java. So a look at how it came into existence and where it stands amongst other programming languages...
Contents
- The Evolution
- The Birth of Java
- What is Java?
- Traditional Programming Model
- How is Java Different?
- How Java addresses Security?
- Java or C++?
- The Java Environment
- Tools of the Trade
- Exercise
- Notes
Before we can begin to write programs in Java, it would be interesting to find out what really is Java, how it came into existence and how does it compare with other computer languages. Also, it is important to know what tools we are going to use for executing programs, from where to get them and how to install them. Here we will briefly understand these things.
The Evolution
Approaches to programming keep evolving all the time. These approaches are more or less driven by the computing needs of those times. When these needs cannot be addressed by languages of that era, a need is felt for a new programming language. These needs have become more and more complex over the years.
In the early days of computing when the need was that a machine should somehow be able to execute instructions, programming was done by manually keying in the binary machine instructions. So long as the programming task was small, programmers were ready to take the pains keying in instructions in binary.
As the tasks became more complex and program lengths increased, need was felt for a new language that could make it easier to write programs. That's when Assembly language was invented. In Assembly, instead of binary, small abbreviations were used to write instructions.
These abbreviations were nothing but representations of binary instructions. This make life much easier for the programmers. The Assembly language programs were very efficient.
As the demands of computing increased, it was felt that learning and using Assembly language are not very easy. To address this need, many languages were invented. These included FORTRAN, BASIC and COBOL. FORTRAN found widespread acceptance in scientific and engineering applications, whereas, COBOL was typically used for building business applications like payrolls, inventory management, etc.
These languages suffered from three important limitations. They are as follows:
(a) They could not be used apart from the domains that they were supposed to serve. So a change in domain necessitated a programmer to learn a new language.
(b) They could not be used to write system-level code that could interact with hardware easily.
(c) All these languages were not designed around structured programming principles. Hence, in programs of sizeable length it became difficult to follow the flow of control.
As a result, a feeling started growing-could there not be a universal programming language that could address all these three concerns? The answer came in the form of C language. It was invented by Dennis Ritchie at AT&T's Bell Laboratories. Since it was designed by a programmer, and not driven by a committee, it addressed the needs of programmers very well. These included speed, efficiency and brevity. Programmers loved it and it soon became a dominant programming language. This dominance continued for almost two decades.
As new hardware evolved, and computers gained widespread acceptance, demands from the program grew multi-fold. The complexity of programs hit the roof, and this is where C language started showing signs of strain. It simply didn't contain those elements that could handle the complexity of the problem being solved. There was a need for a fresh approach to handle the complexity. This gave birth to a new way of organizing the program, called Object Oriented Programming (OOP). C++ was based on these principles and was invented by Bjarne Stroustrup at AT&T's Bell Labs. 1990 was the decade of C++. Since C++ was built on the foundation of C, it became easier for programmers to migrate to this new language quite quickly. It was largely accepted that C++ is a perfect language and there would be possibly no need for a new language. But this belief got dented as you would see in the next section.
In the early days of computing when the need was that a machine should somehow be able to execute instructions, programming was done by manually keying in the binary machine instructions. So long as the programming task was small, programmers were ready to take the pains keying in instructions in binary.
As the tasks became more complex and program lengths increased, need was felt for a new language that could make it easier to write programs. That's when Assembly language was invented. In Assembly, instead of binary, small abbreviations were used to write instructions.
These abbreviations were nothing but representations of binary instructions. This make life much easier for the programmers. The Assembly language programs were very efficient.
As the demands of computing increased, it was felt that learning and using Assembly language are not very easy. To address this need, many languages were invented. These included FORTRAN, BASIC and COBOL. FORTRAN found widespread acceptance in scientific and engineering applications, whereas, COBOL was typically used for building business applications like payrolls, inventory management, etc.
These languages suffered from three important limitations. They are as follows:
(a) They could not be used apart from the domains that they were supposed to serve. So a change in domain necessitated a programmer to learn a new language.
(b) They could not be used to write system-level code that could interact with hardware easily.
(c) All these languages were not designed around structured programming principles. Hence, in programs of sizeable length it became difficult to follow the flow of control.
As a result, a feeling started growing-could there not be a universal programming language that could address all these three concerns? The answer came in the form of C language. It was invented by Dennis Ritchie at AT&T's Bell Laboratories. Since it was designed by a programmer, and not driven by a committee, it addressed the needs of programmers very well. These included speed, efficiency and brevity. Programmers loved it and it soon became a dominant programming language. This dominance continued for almost two decades.
As new hardware evolved, and computers gained widespread acceptance, demands from the program grew multi-fold. The complexity of programs hit the roof, and this is where C language started showing signs of strain. It simply didn't contain those elements that could handle the complexity of the problem being solved. There was a need for a fresh approach to handle the complexity. This gave birth to a new way of organizing the program, called Object Oriented Programming (OOP). C++ was based on these principles and was invented by Bjarne Stroustrup at AT&T's Bell Labs. 1990 was the decade of C++. Since C++ was built on the foundation of C, it became easier for programmers to migrate to this new language quite quickly. It was largely accepted that C++ is a perfect language and there would be possibly no need for a new language. But this belief got dented as you would see in the next section.
The Birth of Java
C and C++ were being used for building most applications till late 1990s. The computing world was more or less divided into three camps-Intel, Macintosh and Solaris. Compilers were available that targeted these microprocessors and created machine language instructions that could get executed on these microprocessors. That was alright for the PC world. However, the microprocessor diversity was too much in consumer electronics world. The microprocessors used in washing machines, microwave ovens and other such devices were so many that creating a full-fledged compiler for each microprocessor was impractical. So a thought started taking shape to create new language that could be used to create software that could run on different microprocessors embedded in various consumer electronic devices. This was the initial motivation that led to the birth of Java.
Thus creation of an architecturally neutral and portable language for consumer electronics devices was the primary factor for Java to come into existence. However, it gained impetus for a very different reason. World Wide Web and the Internet were growing like wildfire, and it's programming needs were similar to those that Java was trying to address. There all types of machines that were getting connected to the Internet. A language was needed that could be used to create programs that could run on machines connected to the Internet and had different microprocessors and operating systems. Java fitted this bill perfectly, because it was designed from ground up with this motive in mind, namely, platform-independence (portability).
So it may not be an exaggeration to state that had Internet and World Wide Web not caught the fancy of the world at the same time in which Java was growing, Java would have possibly remained a language to be used only the consumer electronics world.
With that historic perspective under our belt, I think we are well poised to begin learning Java.
What is Java?
Java is a programming language developed at Sun Microsystems in 1995. It was designed by James Gosling. The language derives much of it's syntax from C++ (and it's predecessor, C), but is simple to use than C++. Reputation of Java has spread wide and far and has captured the imagination of most software professionals. Literally thousands of applications are being built today in Java for different platforms including desktop, web and mobile.
Possibly why Java seems so popular is because it is reliable, portable and easy to use. It has modern constructs that are required to represent today's problems programmatically. Java, like C++ makes use of a principle called Object-Oriented Programming (OOP) to organize the program. The organizing principle has lots of advantages to offer.
Let us now understand how Java achieves portability and reliability.
Traditional Programming Model
When we execute a program on any computing device like PC, Laptop, Tablet or Smartphone, the instructions in it are executed by the microprocessor present in that device. However, the microprocessor cannot understand the instructions written in languages like C, C++ or Java. Hence, these instructions have to be first converted into instructions that can be understood by the microprocessor. These converted instructions are in machine language. This conversion process is known as compilation.
Any running program needs to make use of services of an Operating System (OS) during it's execution. These include services like performing input/output, allocating memory, etc. You must be aware of the fact that on the same microprocessor, different OS can be used. For example, suppose there are two laptops having same Intel Pentium microprocessor. On one laptop one can run Windows whereas on the other, one can run Linux. But since the way these OSs' offer, one can run Linux. But since the way these OSs' offer different services is different, during conversion to machine language these changes have to be accommodated. So for the same program, machine language instructions for Intel + Windows combination would be different than those for Intel + Linux combination. This is shown in Figure 1.2.
The machine language instructions understood by a microprocessor are often called it's Instruction Set. Problem is that instruction sets of different microprocessors are different. Thus, instructions of an Intel microprocessor are different than those of ARM microprocessor. Therefore, any program being executed on a specific microprocessor needs to be converted into machine language instructions which that microprocessor understands. Thus, for the same program, corresponding machine language instructions would be different for different microprocessors. Hence, if a program is compiled for one microprocessor it may not work on another microprocessor it would have to be compiled for that microprocessor again. This is shown in Figure 1.1.
Figure 1.1 and Figure 1.2 depict a depict a scenario called "Write Once, Compile Anywhere". It means to make the same program work on a different microprocessor + OS combination we are not required to rewrite the program, but are required to recompile the program for that microprocessor + OS combination. This is the approach taken by popular traditional languages like C and C++.
How is Java Different?
Java takes a different approach than the traditional approach taken by languages like C and C++. It lets application developers follow a "Compile Once, Run Anywhere" scenario. This means that once a Java program is compiled, it can get executed on different microprocessors + OS combinations without without the need to recompile the program. This makes Java program immensely portable across different microprocessors + OS combinations. The microprocessor + OS combination is often called "Platform". Hence Java is often called a platform-independent language or architecturally neutral language. Java programs are considered portable since they can be used on different microprocessor + OS combination without making any changes in them.
Java achieves this "Compile Once, Run Anywhere" and platform independence magic through a program called Java Virtual Machine (JVM). When we compile Java programs, they are not converted into machine language instructions for a specific microprocessor + OS combination. Instead, our Java program is converted into bytecode instructions. These bytecode instructions are similar to machine code, but are intended to be interpreted by JVM. A JVM provides an environment in which Java bytecode can be executed. Different JVMs are written specially for different host hardware and operating systems. For example, different JVMs are written for Intel + Windows combination, ARM + Linux combination, etc.
During execution, the JVM runtime executes the bytecode by interpreting it using an Interpreter program or compiling it using just-in-time (JIT) compiler. JIT compilers are preferred as they work faster than interpreters. During interpretation or JIT compilation, the bytecode instructions are converted into machine language instructions for the microprocessor + OS combination on which the program is being executed. This perfectly facilitates executing Java programs on different machines connected to Internet.
A Java program is typically stored in a .java file, and the bytecode is usually stored in a .class file. A complex program may consists of many .class files. For easier distribution, these multiple class files may be packaged together in a .jar file (short for Java archive). The working of a Java program discussed above is shown in Figure 1.3.
A Java program is typically stored in a .java file, and the bytecode is usually stored in a .class file. A complex program may consists of many .class files. For easier distribution, these multiple class files may be packaged together in a .jar file (short for Java archive). The working of a Java program discussed above is shown in Figure 1.3.
How Java addresses Security?
Let us first understand what typically happens when we use some web application on the Internet. Through browser on our PC/Laptop/Tablet/Smartphone we use a URL to reach the application present on some web server on the Internet. The web application sends HTML that gets rendered in our browser. However, except for the simplest of web applications, along with the HTML some executable Java program is also sent to our browser. This program is often small and is called Applet. The purpose of the applet is to make the web application more responsive. For example, if we enter a password, it should possible to check whether it follows rules for password creation or not right there within the browser using the downloaded applet, rather than sending the password to server and get it verified. This certainly improves user experience. This is because the check is being performed on the machine itself rather than on the server machine. This saves a roundtrip to the server.
But when we download a applet, there is always a possibility that the applet may contain malicious code like a Virus or Trojan horse that would harm to our machine. JVM prevents this from happening by restricting the applet code from accessing other resources of your machine, other what is supposed to. This makes applets secure. Thus JVM solves two dicey issues in one shot-portability as well as security.
But when we download a applet, there is always a possibility that the applet may contain malicious code like a Virus or Trojan horse that would harm to our machine. JVM prevents this from happening by restricting the applet code from accessing other resources of your machine, other what is supposed to. This makes applets secure. Thus JVM solves two dicey issues in one shot-portability as well as security.
Java or C++?
After learning C, it is often a question whether one should migrate to Java or C++. Answer is both; and that too in any sequence that you want. Though both are Object Oriented Programming languages, neither is an advanced version of the other. Learning one before the other would naturally help to learn the second.
It is important to note that both addresses different sets of problems. C++ primarily addresses complexity, whereas Java addresses portability and security. In my opinion, both languages would continue to rule the hearts of programmers for years to come.
As you start learning Java, you would find that there are many features in it that are similar to C and C++. This is not by accident, but by intent. Java designers knew that they had to provide a smooth transition path to learners of Java language. That's why Java uses a syntax which similar in many ways to that of C and it follows many of the object oriented features of C++, though in a refined fashion.
The Java Environment
We know that JVM contains an Interpreter/JIT that converts bytecode into microprocessor + OS specific machine language instructions. Since instruction sets vary from microprocessor to microprocessor, there exist different JVMs for different platforms. Thus, though any JVM can run any Java program, JVMs themselves are not portable.
JVM is distributed along with a set of standard class libraries that implement the Java Application Programming Interface (API). The Java APIs and JVM together form the Java Runtime Environment (JRE). If your need is only to execute Java programs on your machine, all that you need is JRE. For example, if you wish to play a Java-based game on your machine, you need to install only JRE on your machine for the game to run.
However, if you wish to also develop programs on your machine, you need Java Developer Kit (JDK). JDK contains tools needed to develop the Java programs, as well as JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), Appletviewer, etc. Compiler converts Java code into bytecode. Java application launcher opens a JRE, loads the class, and calls it's main() method. Figure 1.4 shows all these pieces of Java environment.
Tools of the Trade
To create and run Java programs you need to install two software on your PC. These are
(a) Java Development Kit (JDK)
(b) Visual Studio Code (VSCode)
Remember that JDK must be installed before installing Visual Studio Code. There are multiple versions of JDK and Visual Studio Code available for download. Try to download the latest LTS version of JDK and Visual Studio Code (System Installer).
On the download page, select the latest LTS version of Java SE and Visual Studio Code (System Installer) based on the OS (Windows/Mac/Linus) and the Microprocessor (X86 or X64).
JDK is often also call Standard Edition Development Kit or Java SE JDK. Basically JDK contains JVM, JRE, Java compiler and debugger. A compiler is needed to convert the Java program into its equivalent bytecode. A debugger is needed to detect, analyze and eliminate bugs in the program.
When you are developing a Java program you need an editor to type the program. Small Java programs can be typed in one file. But more sophisticated programs may be split across multiple files. To let you type the program, manage multiple files of your program, compile it and debug it, you need a tool that can let you carry out these tasks in a visual and user-friendly manner. This tool is often called an Integrated Development Environment (IDE). One such IDE that is very popularly used for building programs in Java, is Visual Studio Code (VSCode). All programs in this blog have been created using Visual Studio Code (VSCode) IDE.
Once you download the JDK and VSCode you need to install them. This is a fairly simple job and I am sure you would be able to do this easily. If you find any difficulty, installing the JDK and the IDE follow the YouTube videos.
We are now on surer grounds. We now have the historical perspective of what led to creation of Java, what problems it primarily attempts to solve, and what tools we need to install to begin Java program development. It would be a good idea to attempt the exercise next to help you fix these ideas, before we formally begin learning Java language from next post onwards.
(B) Technologies used in Java world for different platforms:
- Desktop - J2SE, Mobile - J2ME, Web - J2EE
(C) Reasons of popularity of Java:
- Same language for varied applications
- Rapid Application Development (RAD) possible
- Easy development cycle
- Easy to manage large projects
(D) Acronyms:
- API = Application Programming Interface
- JVM = Java Virtual Machine
- JRE = Java Runtime Environment
- JDK = Java Development Kit
(E) API = Library of classes in form of packages
(F) JVM = Memory Manager + Interpreter / Just In Time (JIT) compiler
(G) JRE = JVM + API
(H) JDK = JRE + Development tools like javac, java, debugger
(I) VSCode, Netbeans, Eclipse are popular development environments
(J) VSCode, Netbeans and Eclipse internally use javac, java, debugger
(K) Different JREs and JDKs have to be downloaded for different Hardware + OS combination
(L) For execution of Java program only JRE is needed.
(M) To create and execute Java programs JDK + IDE are needed
(N) In C / C++ our program on building is converted into machine language instructions.
(O) In Java on compiling our program is converted into Bytecode instructions.
(P) During execution of Java programs, the Bytecode instructions are converted into machine language instructions and these instructions are executed.
(Q) Bytecode instructions of a .java file are stored in corresponding .class file.
(R) For multiple .java files a .jar (Java Archive) file is created.
(S) To achieve portability C/C++ use "Write Once, Compile Anywhere" principle
(T) To further improve portability Java uses "Compile Once, Run Anywhere" principle
(a) Java Development Kit (JDK)
(b) Visual Studio Code (VSCode)
Remember that JDK must be installed before installing Visual Studio Code. There are multiple versions of JDK and Visual Studio Code available for download. Try to download the latest LTS version of JDK and Visual Studio Code (System Installer).
On the download page, select the latest LTS version of Java SE and Visual Studio Code (System Installer) based on the OS (Windows/Mac/Linus) and the Microprocessor (X86 or X64).
JDK is often also call Standard Edition Development Kit or Java SE JDK. Basically JDK contains JVM, JRE, Java compiler and debugger. A compiler is needed to convert the Java program into its equivalent bytecode. A debugger is needed to detect, analyze and eliminate bugs in the program.
When you are developing a Java program you need an editor to type the program. Small Java programs can be typed in one file. But more sophisticated programs may be split across multiple files. To let you type the program, manage multiple files of your program, compile it and debug it, you need a tool that can let you carry out these tasks in a visual and user-friendly manner. This tool is often called an Integrated Development Environment (IDE). One such IDE that is very popularly used for building programs in Java, is Visual Studio Code (VSCode). All programs in this blog have been created using Visual Studio Code (VSCode) IDE.
Once you download the JDK and VSCode you need to install them. This is a fairly simple job and I am sure you would be able to do this easily. If you find any difficulty, installing the JDK and the IDE follow the YouTube videos.
We are now on surer grounds. We now have the historical perspective of what led to creation of Java, what problems it primarily attempts to solve, and what tools we need to install to begin Java program development. It would be a good idea to attempt the exercise next to help you fix these ideas, before we formally begin learning Java language from next post onwards.
Exercises
Notes:
(A) 2 categories of softwares:
- System Software - OS, Compilers, Device Drivers
- Application Software - Software for desktop/laptop, Web, Mobile
- Desktop - J2SE, Mobile - J2ME, Web - J2EE
(C) Reasons of popularity of Java:
- Same language for varied applications
- Rapid Application Development (RAD) possible
- Easy development cycle
- Easy to manage large projects
(D) Acronyms:
- API = Application Programming Interface
- JVM = Java Virtual Machine
- JRE = Java Runtime Environment
- JDK = Java Development Kit
(E) API = Library of classes in form of packages
(F) JVM = Memory Manager + Interpreter / Just In Time (JIT) compiler
(G) JRE = JVM + API
(H) JDK = JRE + Development tools like javac, java, debugger
(I) VSCode, Netbeans, Eclipse are popular development environments
(J) VSCode, Netbeans and Eclipse internally use javac, java, debugger
(K) Different JREs and JDKs have to be downloaded for different Hardware + OS combination
(L) For execution of Java program only JRE is needed.
(M) To create and execute Java programs JDK + IDE are needed
(N) In C / C++ our program on building is converted into machine language instructions.
(O) In Java on compiling our program is converted into Bytecode instructions.
(P) During execution of Java programs, the Bytecode instructions are converted into machine language instructions and these instructions are executed.
(Q) Bytecode instructions of a .java file are stored in corresponding .class file.
(R) For multiple .java files a .jar (Java Archive) file is created.
(S) To achieve portability C/C++ use "Write Once, Compile Anywhere" principle
(T) To further improve portability Java uses "Compile Once, Run Anywhere" principle
---------------------X---------------------X---------------------




Comments
Post a Comment