Essential Java Interview Questions
Essential Java Interview Questions
In Java, the 'final' keyword is used to declare constants, prevent method overriding, and inheritance for classes. 'Finally' is a block that provides a mechanism to execute code after a try-catch block ends, regardless of whether an exception was thrown or not, typically used for resource cleanup. 'Finalize' is a method that the garbage collector invokes on an object just before it is garbage-collected, allowing for resource deallocation. Each serves a distinct purpose: 'final' focuses on immutability, 'finally' on resource management, and 'finalize' on cleanup operations .
In Java, the String class is immutable, meaning its contents cannot be changed after instantiation, which is efficient in scenarios where the string does not change. StringBuffer and StringBuilder, on the other hand, are mutable, allowing modification of character sequences without creating new objects. StringBuffer is thread-safe as its methods are synchronized, making it suitable for use in multithreaded scenarios, while StringBuilder is not thread-safe but offers better performance in single-threaded contexts due to the lack of synchronization .
HashMap and HashTable in Java both store key-value pairs, but they differ in several key areas. HashMap is non-synchronized and permits one null key and multiple null values, offering better performance in non-threaded applications due to the lack of overhead from synchronization. In contrast, HashTable is synchronized and cannot contain any null key or value, which makes it thread-safe but generally slower in single-threaded scenarios. The choice between the two structures depends on the specific needs in terms of threading: HashMap is preferable when thread safety is not a concern, while HashTable is used when explicit synchronization is required .
Java is considered a platform-independent language because compiled Java programs (bytecode) can run on any device equipped with the Java Virtual Machine (JVM), which interprets the bytecode into machine code specific to the operating system and hardware. Unlike C++, which compiles directly to platform-specific machine code, requiring recompilation for each different platform, Java programs can run on any system with a compatible JVM without modification .
In Java, stack memory is used for static memory allocation and the execution of threads. Each thread in Java has its own stack, storing local variables, method calls, and partial results. Heap memory, in contrast, is used for dynamic memory allocation for Java objects and JRE classes. Memory in the heap is shared among all threads. Java utilizes the stack for primitive variables and references, and the heap is utilized for creating new objects .
Java is not considered a pure object-oriented language because it supports primitive data types such as int, float, char, etc., which are not objects. A purely object-oriented language treats everything as an object, whereas Java's inclusion of primitive types is intended to provide performance benefits as objects are generally more expensive in terms of memory usage and processing speed. Despite this, Java maintains most object-oriented principles, allowing object creation for complex data manipulations and using features like classes and objects, encapsulation, inheritance, and polymorphism .
Data encapsulation in Java is the principle of restricting access to certain components of an object and only exposing a limited set of functionalities. This is achieved using access modifiers such as private, protected, and public. Encapsulation ensures that internal representation of an object is hidden from the outside and only accessible or modified through well-defined interfaces, thereby enhancing the security and robustness by protecting the state of the object from unintended interference and misuse .
Inheritance in Java is a mechanism where one class acquires the properties and behaviors (methods) of another class. It enables the creation of a new class that inherits fields and methods from an existing class. The main types of inheritance in Java are single inheritance, multiple inheritance (through interfaces), and multilevel inheritance. Inheritance allows for polymorphism, where a subclass can define its own unique behaviors while sharing some of the same functionalities of its superclass, exemplified by method overriding where a subclass provides a specific implementation for a method already defined in its superclass .
The Java Development Kit (JDK) is a full-featured software development kit used for developing Java applications; it includes the JRE, compilers, and tools like JavaDoc and Java debugger. The Java Runtime Environment (JRE) provides the libraries, Java Virtual Machine (JVM), and other components necessary for running Java applications but does not include development tools. The JVM is an abstract computing machine that enables a computer to run Java programs by converting bytecode into machine code .
Java employs an automatic garbage collection process which manages memory by reclaiming memory used by objects that are no longer accessible or needed. This contrasts with the manual memory management in C++, where the programmer must explicitly deallocate memory. The advantage of Java's garbage collection is that it reduces the risk of memory leaks and frees developers from the complexities associated with manual memory management, allowing them to focus on application logic rather than memory concerns .