Java is born with some unique and interesting features that made it most powerful and demanded language of the times. Following are the features of Java.
"Java compiled file known as .class file can be executed on any OS without bothering on which platform (OS) the source code was compiled". That is, a Java source file can be compiled on any OS and executed on the same or any other OS. This is quiet contradictory to other languages like C/C++ where these languages should be executed on the same OS where they were compiled earlier. This Java feature is very unique and is unparalled by any other language from the day Java came into its existence, the year 1995; a period of 16 years.
Java's Magic Touch – Bytecode
Bytecode is a new term to the programming world introduced by Java. The compilation product of Java source file is a .class file consisting of bytecode. This bytecode makes Java platform-independent where as the binary code of C/C++ makes C/C++ platform-dependent.
The bytecode is not an ultimate format that is readily understood by the microprocessor. It must be converted into binary code format that can be processed by the microprocessor directly. This conversion is taken care by the JVM. The format conversion takes some time and thereby Java executes slower. Incase of C/C++, they contain binary code directly. The slow nature is overcome to some extent with the introduction of JIT (Just-In-Time) compiler . Following figure gives the concept.
Java Compiler – Is Platform-Independent?
A compiler job is to compile the source code into an executable file. An executable file, as the name indicates, gives output when executed. Every OS comes with its own set of functions and for every OS operation, the OS makes a function call to do the job. The functions may relate to the resource management like file system, memory and thread operations. Every OS comes with its own functions written in proprietory code. The compiler should thoroughly understand these functions. For this reason, the compilers are different for different operating systems. For example, a C/C++ compiler developed for UNIX understands the functions of UNIX but not that of Windows.
For the above rule, Java compiler is not an exception. A Java compiler developed and executed on UNIX does not work on Macintosh etc. That is, Java compiler, like any other compiler, is platform-dependent. The difference with Java compiler is it produces a platform-independent executable file known as .class file. Following figure explains.
Note: The .class file is not really an executable file because it must be converted in binary code specific to the OS at execution time.
Portability for Java comes from its bytecode. A Java .class file, obtained from the compilation on one platform can be ported or carried, for execution, to a different platform. The necessity is to have a compatible JVM (specific to the platform). JDKs are different for different platforms.
3. Simple and Familiar
The main motto of Java designers is to take out the language very simple to practice and develop the code. Many confusing concepts of C++ are simply deleted from Java and right now Java proved that a language can exist without these concepts. Following list gives a few concepts not supported by Java but supported by other object-oriented languages like C++.
1. No support for pointers.
2. No "goto" keyword. The minus point of "goto" is that you loose the command on control flow of the code and thereby debugging becomes a nightmare.
3. No support for unsigned data types.
4. No operator overloading.
5. No preprocessor directives (# define).
6. No multiple inheritance.
7. No garbage values for unassigned data types.
8. No explicit memory management by the developer. Memory management is taken care implicitly.
9. No structures, no templates and no unions.
Java looks as if it is a known language, familiar earlier, as much of the syntax is borrowed from C/C++. The control structures, functions, arrays and data types etc. looks the same of C/C++. Even though it is a derivative of C++, Java is fully an independent language where the execution entity is a class.
Encapsulation, Inheritance and Polymorphism, the three object-oriented principles are duly supported by Java. Object-oriented methodology aims at reusability of code written only once earlier. Java supports reusability through composition and inheritance. Everything can be viewed as an object; to achieve this, the Java API includes a plethora of classes like java.lang.Integer (to treat a primitive data type as an object), java.io.File (to view an ordinary text file as an object) and java.net.URL (to convert an IP address of a system into an object) etc. Apart this, Java supports dynamic binding and static binding of methods and object casting etc.
Benefits of OOPs
OOPS comes with many advantages over procedure-oriented languages useful for product architects and programmers. The main and foremost advantage is productivity with low-maintenance cost. Following list illustrates a few important.
1. With Java design patterns, the product architects can develop modules that can be integrated later to evolve as a big reusable component.
2. Using objects, a programmer can divide his work into a number entities (units) and coding each entity separately.
3. Data hiding helps to hide the data of one entity (unit or object) from another. This helps to write secured programs that cannot be accessed by other parts of the code.
4. With encapsulation, the same data (variables) can co-exist with different objects but without overlapping and ambiguity.
5. Due to encapsulation, software complexity can be reduced.
6. Inheritance helps reusability of code by avoiding redundancy.
7. Object-oriented systems can be scaled up easily from small to medium or medium to large business systems.
8. Using objects, message servicing systems can be developed easily to integrate modules of a business process.
5. Architecture Neutral
We know earlier, the Java compiler generates an intermediary bytecode that must be again converted into binary code at execution time. This binary code conversion is effected by the JVM. The bytecode is OS architecture neutral. Any OS can execute the bytecode without bothering its architecture.
Java is a robust language which avoids many mistakes a programmer can commit at coding level. First and foremost towards this goal is exception handling mechanism which takes care of user's mistakes at runtime. Java includes a strong type checking for objects and data types both at compile time and runtime. Variables must be assigned before they are used. Array size should not be given in initialization.
Java comes with implicit memory management which takes care of many mistakes a programmer can commit in coding. Java eliminates the memory manipulations functions like free() or delete() etc.
Java started its development for Web-based communication. Java is a strong supporter of Web. Java comes with special programs, Applets and Servlets useful for Internet (Web) communication.
Applets placed on client-side can communicate with servlets placed on server-side. Applets written in Java can be downloaded from any Web server and executed on any client system irrespective of OS.
8. Implicit Memory Management
We know earlier, one of the striking features of Java is its implicit memory management. It avoids much of the tedious code of memory management by the programmer. If a programmer does not manage the memory robustly with utmost care, as in C/C++, the system may crash sometimes while executing the program. The programmer is relieved of the usage of destructors and many functions like malloc(), calloc(), sizeof(), free() and delete() etc.
When Java software starts, it spawns a garbage collector (gc) thread that takes cares of the garbage of a process. The objects not required in further process of a program are treated as garbage. Keeping the memory for unwanted objects is a wastage. The garbage collector removes these garbage objects while execution is going on; even without a message or warning to the programmer.
Generally, a language like C/C++ is compiled or a HTML is parsed or VBScript is interpreted; but Java is both compiled and interpreted. At compilation stage, the Java source code is converted into a bytecode file. As bytecode cannot be executed by a microprocessor, at runtime, the JVM converts the bytecode into binary code. This platform-specific binary code is executed and generates output. That is, in Java, from source code to the output, two stages exist – compilation and interpretation.
No language can parallel Java in networking support. Java supports both LAN programming (TCP/IP and UDP) and distributed programming. In distributed computing, the systems in communication are spread across the globe. Again this is of two types – Internet-based and non internet-based. Java includes sockets and server sockets to support socket (LAN) programming (with java.net package), servlets and JSP (with javax.servlet.http package) for Internet-based programming and RMI/EJB (with java.rmi and javax.ejb packages) to support distributed non Internet-based communication. Java wisely knows the utilization of HTTP, SMTP, POP3 and FTP protocols etc. to communicate between systems.
For latest computing techniques, multitasking is a must as in multimedia (animation). Multitasking is a concept supported by OS. A language can utilize effectively the multitasking capabilities of an OS. Java too feels its necessity and supports through its built-in classes from java.lang package. Java supports multitasking through multithreading. A single Java process can be divided into multiple threads and each thread can be given a different task. If one thread's execution stops for some reason, the microprocessor can be assigned with the task of another thread. This shifting of threads reduces microprocessor idle time and thereby output is given at an earliest and looks at the same time.
Generally in coding, one class may require the functionality of another class. At runtime, JRE can search, hook and load the necessary classes required for the smooth execution of the program. Do not bother where the files are, may be on your own local disk or in a server at a far off place, JRE knows well how to play the game dynamically. Java has got built-in support for many protocols like HTTP and FTP using which the JRE can download the classes, files and libraries required for the execution of a program.
Java, with its good support for distributed technology, comes with many security features required in programming. Java does not support pointers and thereby avoids accidental erasures or changes of the values existing in a different location. Bytecode verifier checks the permissions in between the classes whether they can access or not. Java includes encryption algorithms that can be used by the programmer in distributed communication. Applets are not permitted to access the hard disk files and software.
We know earlier, Java is slower in its execution. To enhance the performance, the designers took utmost care in designing the API libraries like introducing StringBuffer in place of String, StringBuilder in place of StringBuffer, lightweight Swing GUI components in place of AWT heavyweight components. ArrayList (methods are not synchronized) can be used for Vector (methods are synchronized).
Also learn Java drawbacks.