Java - Object-Orientation
A key feature that defines Java's essence and structure
Object Orientation (OO) is a key feature of Java which defines its essence and structure. Object Orientation refers to a way of designing a language and a way of programming. OO systems attempts to simulate a facet of real life to make programming more intuitive and to simplify complex structures that hold special data. In Object Oriented Programming (OOP), data is always combined with the operations that involve the data. ‘Objects’ may be envisioned as data stored along with information about the data's 'behavior'.
All entities are considered objects in an object oriented model. An entity is contained in another object and may itself contain many 'smaller' objects, just like in real life. For example, a cupboard is an object. It is contained in a room - a larger object - and by itself this cupboard also contains shoes, umbrellas, and coats. Objects have properties. A cupboard may have a color property, a size property, and perhaps a “used-for-storing-what” property. Also, objects may have a sequence of actions performed on them to some end (open the cupboard, for example). Such actions are called the object's methods. Each object may contain member objects, properties, and methods. Suppose a cupboard object is defined in the following way: a cupboard consists of - one or more shelf objects as members; has color, size, and 'contains' properties; and methods such as open, close, add and remove. We may, in an OOP, declare a large, brown, book cupboard with five shelves or a small white shoe cupboard with three shelves. Each of these cupboards are said to be the instances of the object. An instance object is sometimes conceptually similar to a proper noun; e.g. Rover is an instance of dog, Somalia is an instance of country.
Everything in Java is represented as an object with the exception of atomic datatypes like numbers, characters and boolean (true-false) values. Also, most programs in Java contain the definition of an object, its properties, members, and methods.
OOP promotes reusable code through generic object definitions. For example, a detailed definition of a 'bank account' object with an extensive list of properties and methods may be created by a single group. Organizations across the board may save time and money by reusing this object rather than designing and implementing a custom bank account object.
Fig 1a: Cupboard object representation in a Generic Language
The process of getting a computer program to work is as follows: First, the commands meant to execute the task or tasks under consideration are written in the programming language of choice, making sure that the syntax and rules specified for the chosen language are strictly adhered to. Next, a compiler that came with the language is used to read the program, confirm its adherence to the syntax, and translate it to a format that the operating system (such as Windows XP or Linux which are protocols used to 'speak' to the computer) understands. This process is called compilation. If the code within the program is wrong, compilation fails and error messages are displayed to the user; who then sets about debugging or correcting the program. The final version of the compiled code will be in Binary format, which only makes sense to the operating system and the environment on which it was compiled. This compiled code is sometimes called an executable, which when executed or ran becomes known as an application. The original program is called the source code.
Fig 1b: Creating an Application
The main advantage of Java is that its compilation results in machine-independent bytecodes. This bytecode is the same whether the Java program was compiled using a mac, a solaris machine running linux, an IBM server, or a personal computer running Windows XP. This feature is referred to as Platform Independence. The marked preference for Java in application development circles and its ubiquity are in large part a result of the platform independence of Java applications. Java applications run on a Java Virtual Machine (JVM). For all practical purposes, the JVM provides the environment on which the Java application runs. The JVM is itself an application that interprets and executes Java bytecodes. JVM is written in a language (native code) appropriate to the Operating System and hardware on which it is meant to run. JVMs exist for any computer and operating system, such as Win 32 personal computers running Windows, systems running Solaris, palm pilots, and even cell phones. The JVM that Java applications run on differs with the OS and the hardware; as Java applications comply with the famous write once, run anywhere paradigm.
Just-in-time compilation (JIT) is a technique that is often used to translate the Java bytecode generated during compilation into the native code of the Operating System at the time of execution. The JVM assesses the currently running program and selectively recompiles and optimizes parts of it to increase efficiency during a process called 'dynamic recompilation'. Optimization of this sort is often tailored to suit the runtime environment and is therefore superior to the optimization at the time of compilation. Java applications therefore run at par with native codes due to JIT compilation and dynamic recompilation. Internet related applications, server-side applications, and embedded systems on a wide variety of consumer products are also a credit to Java Technology's portability.
Although Java is a very powerful Object-oriented language, it is a lot easier to learn as a 'first' language than other languages like C++. Research and Metrics show that programs written in Java tend to be more compact than their identical counterparts written in other major languages. Also, Java has an extensive built-in network configuration and support capacities that makes it the most intuitive choice for devising network oriented solutions.
A program in Java may use several packages. A package is a sort of library or collection of Java software components that are related to each other and perform similar functions. For example, AWT or 'abstract windowing toolkit', used to write applets, is part of one package while Java Servlet technology, used by servers to communicate with client computers across the Internet, is part of another. All of Java is made up of such packages, and all the packages together form the Application Programming Interface (API). Individual programmers and developers who require some functionality only partially supported by a package may easily extend or augment the API with custom packages to perform additional functions. There is a wealth of free custom packages available for use. The chances that a Java programmer will find a tested and tried package to perform a custom task are very high; in most cases, there is no need to expend time on writing, debugging, and testing new programs. In addition to Object-orientation, the API based, easily extensible structure of Java encourages good coding practices.
Memory leaks may occur in languages where programmers allocate memory to create and store objects (sequences of data made up of numbers, strings and so on) at runtime. The developer has to manually de-allocate memory in the program once these objects become unnecessary. If a programmer forgets to de-allocate memory, a memory leak occurs and the program will consume a huge amount of memory trying to store the unnecessary objects. At such point, the program may simply crash and exit with a core dump or freeze up the computer on which it is running. Also, programs become unstable if a region of memory is de-allocated twice. Memory-related code makes a program more complex and hard to understand and maintain. Programmers are sometimes restrained to using a few tested designs to ensure that memory leaks do not happen
Java also offers automatic garbage collection. The programmer simply creates the objects while the Java Runtime Environment (JRE) is responsible for managing the object's lifecycle. The JRE figures out whether an object is in use or not by keeping track of all the active references to it from other objects. The Java garbage collector automatically frees the memory consumed by 'unreachable' objects to which it can no longer find any references. This process prevents memory leaks. Memory leaks may still occur due to sloppy programming, such as when a program contains references to unnecessary objects, however, automatic garbage collection adds flexibility to programming in Java. Garbage collection is built into Java and is practically invisible to the programmer.
Finally, incorporating viruses or trojans into a Java program with a view to infect remote computers on which the program may run is virtually impossible. For one, a virtual machine architecture allows a degree of control over the sort of operations and codes executing on the JVM. Also, the VM does not allow remote Java code to run anywhere other than a restricted "sandbox", thus protecting the user from malicious code. A "safe" digital signature should be acquired by publishers who wish to leave the sandbox and access the local file system and network.
The Java Runtime Environment (JRE)
The Java Runtime Environment should be installed before running Java applications. One common use of the JRE is as a web browser plug-in that allows Java-technology related applications to run on web pages viewed in the user's computer. Through the java.com site, computer users can check to see if their JRE is updated, and download the current JRE version. Basically, the JRE software bundles a version of the Java Virtual Machine (JVM) for the user's OS and hardware, as well as the appropriate Application and Programming Interface (API - a set of standard software components). Applications will not run until the JVM and API versions match; this is why they are bundled together. The API and Java Virtual Machine form a cushion between the Java program, the hardware and the OS of the computer.
Fig 1d: The JRE's Platform Independence