The most basic question asked to a beginner in a Java Interview is “What are the advantages of Java Programming Language?”. Here is a detailed answer to this question.
Java is a relatively high level language. It provides lot of features out of the box, which makes it easy to learn for today’s developers and focus on the real business implementation rather than worrying about the system level infrastructure management.
Few of the advantages of Java Programming Language are:
- Platform Independence : Java is platform independent, which means that if a program is written and compiled in Java on any platform(underlying hardware and software), it can run on any other platform of similar capabilities subjected to available hardware (computing power, memory and disk space) and software(installed JRE) requirements. This is achieved by the virtue of the software component which is called Java Virtual Machine which is an abstract computing machine, when I say abstract I mean it can have many implementations. Oracle provides one such implementation.
- Automatic Storage Management : This is done using a garbage collector, which avoids the safety problems of explicit de-allocations. This means that a programmer need not call the destructor (as in C/C++) to explicitly de allocate the memory used by an the structures or objects. In Java when an object is no longer referenced, it may be reclaimed by the garbage collector. The programmer cannot force the garbage collection event, the JVM triggers it as per the need. Java also provides capabilities to objects to perform some cleaning work (if required) like freeing connections and other system resources before it is garbage collected. Java with the help of JVM also unloads classes if they are no longer needed in the execution.
- Avoids Unsafe Constructs : In Java arrays are accessed after explicitly doing an index check on the bounds of the array. It throws an
ArrayIndexOutOfBoundexception in case the program tries to access an index which is not within the range of the array. This helps in running the program correctly, else imagine a scenario where the language does not check the array index and returns a value which is stored in the memory location, but the memory location is not within the boundaries of the array. This could pose a big question on the correctness of your program.
- No explicit declaration order required : It does not require types (Classes or Interfaces) or their members(fields and methods) to be declared before they are used. It only becomes significant when we declare local variables, local classes and the order of initializers of fields in a class or interface. All other variables are automatically initialized to a default value, the Java programming language does not automatically initialize local variables.
- Type Safety : Java is a safe programming language because of its static typed and strongly typed nature. Statically typed means every variable is declared and has a type and the type is known at the compile time. Strongly typed means that a variable can only be assigned a set of values which are compatible with the type of the variable. This also helps in identifying most of the errors at compile time and clearly distinguishes them with the runtime errors.
- Exception Handling Support : Java has defined an Exception Hierarchy and has a great support for Exception Handling. This offers a lot of robustness and helps the developers to handle compile time exceptions without fail and write a defensive program.
- Multi-threading Support : Java encapsulates the underlying system’s thread infrastructure and offers a great and easy support for multi threading. There is no explicit need of writing a monitor code for acquiring a locks on objects. Java also makes it easy to create multiple threads and execute independent tasks with the help of Thread Pools.
If you want to learn these aspects in more detail look at Core Java section
Happy Learning & Stay Subscribed