Java Programming Revision Notes
Java Programming Revision Notes
Java applets are designed to be executed within a web browser context and come with stringent security restrictions due to their execution from remote sources. These restrictions are enforced by the Java Security Manager to prevent potentially harmful operations like file access on the client machine. In contrast, standalone applications run on the user's local machine and have full access to the system resources, allowing them to perform tasks without the same security constraints. This fundamental difference affects how they are developed; applets must be tested rigorously within the browser's sandbox environment .
Java achieves platform independence through its use of the Java Virtual Machine (JVM), which abstracts the underlying hardware and operating system specifics. When a Java program is compiled, it is converted into byte code that can be executed by the JVM. Different JVM implementations exist for different platforms, allowing the same Java byte code to run on any operating system that has a corresponding JVM. This separation of the byte code interpretation from direct hardware execution ensures that Java programs are write-once, run-anywhere .
The Java Collections Framework (JCF) is crucial for performance and flexibility in Java applications. It standardizes the way groups of objects are stored, retrieved, and manipulated. Collections like List, Set, and Map offer flexible ways to handle data, allowing dynamic arrays (List), unique element storage (Set), and key-value mappings (Map). The framework provides efficient algorithms for sorting, searching, and manipulating collections, optimizing performance compared to manually implemented structures. Additionally, JCF supports thread-safe variants and concurrent collections, offering scalability in multithreaded environments. This combination of flexibility, efficiency, and scalability makes JCF an integral part of Java programming .
Java handles exceptions using the try-catch mechanism, allowing code that might throw exceptions to be enclosed in a 'try' block followed by one or more 'catch' blocks that handle specific exceptions. For example, attempting to divide by zero can be caught by catching an ArithmeticException: 'try { int x = 10 / 0; } catch (ArithmeticException e) { System.out.println("Error: " + e); }'. Custom exceptions can be created by extending the Exception class to handle specific scenarios, as in 'class MyException extends Exception { MyException(String msg) { super(msg); } }', which can then be used similarly within the try-catch mechanism .
Encapsulation in Java is a fundamental principle of object-oriented programming that restricts access to certain components of an object and prevents unauthorized modifications. By using private fields and providing public getters and setters, encapsulation enables controlled access to an object's data. This improves code maintainability by reducing dependency on the specific implementation details, facilitating changes without affecting other parts of the program. Additionally, encapsulation enhances security by protecting object state and encourages a modular programming approach, where objects can be easily reused and tested independently .
Constructors in Java are special methods used to initialize objects when they are instantiated. They have the same name as the class and do not have a return type. Constructors can be overloaded, meaning that a class can have multiple constructors with different parameter lists, allowing different ways to initialize a class's objects. For instance, 'class Student { int id; String name; Student(int i, String n) { id = i; name = n; } }' shows an overloaded constructor allowing instantiation with specific values for 'id' and 'name' . This enables flexibility in object creation and initialization strategies .
Method overloading and method overriding are both forms of polymorphism in Java but differ in how they achieve it. Method overloading occurs within the same class and involves methods that have the same name but different parameter lists. It allows different functionalities based on different input parameter types or counts. Method overriding occurs in a subclass and involves redefining a superclass method with the same method signature, enabling runtime polymorphism. Overriding allows an object to invoke methods based on its actual subtype rather than the reference type, demonstrating dynamic polymorphism .
JDBC (Java Database Connectivity) is a set of Java APIs that enables Java programs to interact with databases. To connect to a database, a driver class must be loaded using 'Class.forName("com.mysql.cj.jdbc.Driver");'. A connection to the database is then established with 'Connection con = DriverManager.getConnection(url, user, pass);'. Once connected, PreparedStatement objects can execute SQL queries. For example, inserting a record uses 'PreparedStatement pst = con.prepareStatement("INSERT INTO table VALUES (?, ?)"); pst.setInt(1, 101); pst.setString(2, "Buddy"); pst.executeUpdate();'. JDBC handles SQL exceptions and provides metadata about the database .
Java's Abstract Window Toolkit (AWT) provides a set of components for building graphical user interfaces (GUIs) and is part of the Java Foundation Classes (JFC). It relies on native system components (peers) for rendering UI elements, offering less consistency in appearance across different platforms. Swing, on the other hand, builds on AWT but offers lightweight, platform-independent components purely drawn in Java, providing a more consistent look and feel. While AWT might be faster due to direct native resource usage, Swing offers richer features like pluggable look-and-feel, more complex components like trees and text areas, and better event handling capabilities .
The 'synchronized' keyword in Java is used to control access to blocks of code or methods to ensure that only one thread at a time can execute the synchronized section. This is essential for thread safety, as it prevents race conditions where multiple threads could potentially modify shared resources concurrently, leading to inconsistent states. By synchronizing a method or a code block, Java locks on the object, ensuring that critical sections are executed in a mutually exclusive manner. This mechanism, while ensuring data consistency, can introduce performance overhead due to increased locking and context switching costs .