6 Matching Annotations
  1. Last 7 days
    1. The clone() method creates a new array with its own memory but contains references to the same objects for non-primitive data types.

      Q. Meaning the array is new but the elements in the array point to the same location as that of the original?? so if i access an element and mutate the element the element in the original array also gets affected?

      That is accurate—this behavior is known as a shallow copy.

      When you clone an array of objects in Java:

      Mutating the object's internal state affects both arrays because both array slots point to the exact same object in memory.

      Reassigning the reference at an array index affects only that array, because the array containers themselves are separate objects.

      Q. so we can add new elements without the other knowing that's the main usecase of the clone

      Yes, that is a primary use case—isolating the array structure and references so one array can be modified, reordered, or reassigned without affecting the other. This pattern is commonly known as defensive copying

    2. manipulate

      Doesnot mean immutable: When you perform a "manipulation" on a String—such as .toUpperCase(), .concat(), or .replace()—Java never modifies the original String object. Instead, it generates and returns a brand-new String object containing the transformed text, leaving the original object completely untouched.

    3. 44. What is covariant return type?A covariant return type allows an overridden method in a subclass to return an object of the subclass type instead of the superclass type.It is used in method overriding. The return type of the child class method must be a subclass of the parent class return type. It improves code readability and type safety

      ``` class Animal {} class Dog extends Animal {}

      class Parent { Animal getAnimal() { return new Animal(); } }

      class Child extends Parent { @Override Dog getAnimal() { // Covariant: Dog is a subclass of Animal return new Dog(); } } ```

      ``` import java.util.List; import java.util.ArrayList;

      class DataProvider { List<String> fetchData() { return new ArrayList<>(); } }

      class FastDataProvider extends DataProvider { @Override ArrayList<String> fetchData() { // Covariant: ArrayList implements List return new ArrayList<>(); } } ```

    4. Filter Streams are special stream classes in Java that add extra functionality to existing input and output streams. They wrap another stream and process the data while it is being read or written.Filter streams enhance stream operations such as buffering, data conversion, and object serialization. They do not directly connect to a data source or destination; instead, they work on top of another stream

      Primary Domain * Reading and writing raw bytes/characters to files, network sockets, or memory.

      What "Filter" Means * Wraps/decorates an existing I/O stream to add features (e.g., buffering, primitive data parsing, or encryption).

      Design Pattern * Decorator Pattern (wraps an underlying InputStream or OutputStream).

      Example // FileInputStream provides raw byte reading. // BufferedInputStream (a FilterStream) wraps it to add buffer capabilities for speed. InputStream fileStream = new FileInputStream("data.txt"); InputStream bufferedStream = new BufferedInputStream(fileStream);