Java Exam Notes: Key Topics & Questions
Java Exam Notes: Key Topics & Questions
In Java, 'throw' is used to explicitly throw an exception from a method or any block of code. It is typically used for custom exceptions. On the other hand, 'throws' is used in a method signature to declare that a method may potentially throw one or more specified exceptions. Understanding the distinction is critical for error management because 'throw' initiates an exception manually, while 'throws' alerts the caller of the method about potential exceptions it needs to handle or declare, promoting robust error handling strategies .
JDK (Java Development Kit) is a software development environment used for developing Java applications and applets. It includes a private JVM (Java Virtual Machine), a variety of development tools, debuggers, and libraries. JVM, on the other hand, is an engine that provides a runtime environment to execute Java bytecode. It is part of the JRE (Java Runtime Environment), which supplies libraries and other components that are necessary for running applications written in Java. The significance of these components lies in their ability to allow developers to write, compile, and run Java applications across different platforms without platform-specific modifications .
In Java, both abstract classes and interfaces are used to declare methods that subclasses must implement but are different in their usage and implementation. Abstract classes can include both abstract methods and concrete methods, allowing for some code reuse and complex class structures. Interfaces, however, can only contain method declarations (unless they include default or static methods, available from Java 8) and constants, suggesting a 'contract' of behaviors a class must implement. Abstract classes are appropriate when there's a shared base of functionality among classes, while interfaces are suited for defining capabilities shared across otherwise unrelated classes .
The 'this' keyword in Java is a reference variable that refers to the current object. It is used to avoid namespace collision that may occur when instance variables and parameters share the same name. By using 'this', constructors and methods can distinguish between class attributes and parameters with the same name. For instance, 'this.x = x;' assigns the parameter 'x' to the instance variable 'x' in a class. Employing 'this' enhances code readability and clarity in complex class hierarchies .
Dynamic method dispatch is a mechanism by which a call to an overridden method is resolved at runtime rather than compile-time. It is a crucial aspect of achieving runtime polymorphism in Java. When an overridden method is called through a superclass reference, Java determines which version of the method (superclass or subclass) to execute based on the actual object's type that the reference is pointing to at runtime. This enables Java to behave polymorphically, allowing one interface to point to a method in a class, leading to flexible and manageable code .
Java does not support multiple inheritance to prevent the complexity and ambiguity that arises from the 'diamond problem,' where a class inherits the same method from multiple sources. Instead, Java provides interfaces as a way to achieve similar functionality. A Java class can implement multiple interfaces, each of which can declare abstract methods without giving them any implementation. This allows a single class to inherit behaviors from multiple sources (interfaces) while avoiding the inherent problems associated with multiple inheritance .
Synchronization in Java is crucial in a multithreaded program to control the access of multiple threads to shared resources. Without synchronization, two or more concurrent threads could potentially modify shared resources, leading to inconsistent results, data corruption, or deadlocks where two threads wait indefinitely for each other to release resources. Proper synchronization ensures that only one thread accesses a critical section of code at a time, maintaining data integrity and consistency during execution .
In Java, the 'static' keyword denotes that the particular member belongs to a type itself rather than to an instance of that type. This means that static members are shared between all instances of a class. When a member is declared static, it is allocated memory once at the time of class loading, which means the memory isn't continually consumed as objects are instantiated. This impacts the program's memory allocation by reducing redundancy and potential memory wasting, as static members are not tied to any instantiated object .
In Java, the String class represents immutable character strings, while the StringBuffer class represents a mutable sequence of characters. This immutability in String means every modification of a string object results in a new object being created, which can be inefficient in scenarios of frequent modifications. In contrast, StringBuffer is designed for use when there are a lot of mutable strings' operations, as it can change its content without creating new objects, leading to better performance with large modifications .
Java's platform independence is primarily achieved through the use of bytecode, which is an intermediate representation of the code that is executed by the Java Virtual Machine (JVM). When a Java program is compiled, it is transformed into a platform-independent bytecode file, which the JVM can execute on any device or operating system that has a compatible JVM implementation. This means the same Java application can run on different platforms without any required modifications, making Java a 'write once, run anywhere' language .