Java Arrays and Collections Cheat Sheet
Java Arrays and Collections Cheat Sheet
ArrayList and LinkedList in Java are both implementations of the List interface but have different performance characteristics. ArrayList is backed by a dynamic array, making it better for frequent retrieval operations and faster when accessing elements by index. LinkedList, however, is a doubly linked list, which makes insertion and deletion of elements faster compared to ArrayList, as these operations only involve adjusting the pointers rather than shifting elements. The choice between them depends on use cases: ArrayList for faster iteration and random access, LinkedList for frequently adding/removing elements .
Constructors in Java can be no-args (without parameters), parameterized (with parameters), or default. A no-args constructor is used to initialize default values, which is critical in frameworks like serialization and Hibernate. Parameterized constructors allow different initializations by providing different sets of input. The default constructor, if not explicitly defined, is provided by Java if no other constructor is present. Having explicit constructors helps in clear initialization strategies and the 'super' keyword is often used to leverage parent class constructor functionalities .
Upcasting in Java involves casting an object to a superclass reference and is implicit. It allows accessing only the superclass methods, including overridden methods in the subclass. Downcasting is casting back to a subclass and is explicit, often requiring a check using 'instanceof' to avoid ClassCastException. Upcasting is useful for polymorphism where a parent reference can point to child objects, while downcasting is necessary when specific child class method access is needed. Care must be taken to ensure the object is actually an instance of the subclass when downcasting .
Stacks and Queues are fundamental data structures in Java. A Stack operates on a last-in, first-out (LIFO) principle, ideal for scenarios requiring reverse order processing like function call management. A typical use is the browser back button implementation, where recent pages are popped off. A Queue, on the other hand, operates on a first-in, first-out (FIFO) principle, suitable for scheduling or buffering scenarios like print queue management, where tasks are processed in the order of arrival .
The 'finally' block in Java is used to execute important code such as closing resources or clean-up code, regardless of whether an exception is thrown or not. It ensures that the code within runs after the execution of a 'try' block and any matching 'catch' block, if present. Unlike 'catch', it cannot catch exceptions; it's primarily for resource management .
Sorting complex objects in Java involves implementing the interface Comparable for natural ordering, where you can override the compareTo method to specify the sorting logic based on properties like weight. For alternative ordering, you can use the Comparator interface, where you specify the ordering logic in a separate class. Arrays.sort() can be used with either Comparable or Comparator to sort the array or collection .
The 'Comparable' interface in Java provides a single method, compareTo, which defines the natural ordering of objects that implement it. This is typically used directly within the class. For instance, a 'Person' class could implement Comparable based on weight. In contrast, 'Comparator' allows for defining multiple sort orders by implementing its compare method in separate classes. For example, sorting people based on name can be done using a Comparator implemented in a class named SortOnName. Comparable is used for one primary natural order, while Comparator is flexible for alternate ordering criteria .
In Java, the 'final' keyword is used to denote constant behavior. When applied to variables, it makes them constant, so their value can only be assigned once. Applied to methods, it prevents overriding in subclasses, ensuring the method's behavior stays unchanged. When applied to classes, it means the class cannot be subclassed, which is crucial for immutability and security purposes. This keyword is used to exert control over inheritance and state mutation .
Type casting in Java is a way to convert a variable of one data type into another. Widening casting is automatic, converting a smaller type to a larger type, such as int to double. Example: int myInt = 9; double myDouble = myInt. Narrowing casting requires explicit conversion, changing a larger type to a smaller type, such as double to int. Example: double myDouble = 9.78; int myInt = (int) myDouble .
In Java, 'public' means the member is accessible from any other class. 'Protected' allows access within the same package and by subclasses. 'Default', also known as package-private, means the member is only accessible within its own package. 'Private' restricts access to within the defining class itself. These modifiers control the visibility and encapsulation of class members, playing a crucial role in data hiding and object-oriented design .