Close Menu
Şevket Ayaksız
    What's Hot

    Java Array Copies: A New Array Does Not Always Mean Independent Objects

    November 3, 2026

    Oracle COUNT: Define the Metric Before Choosing the Expression

    November 3, 2026

    Java Streams Are Single-Use: Rebuild the Pipeline for a Second Result

    November 3, 2026
    • software
    • Gadgets
    Şevket AyaksızŞevket Ayaksız
    • Home
    • Technology

      Apple: iPhone 17 and Older Models Hit With Unexpected $100 Price Increase

      September 10, 2026

      Apple Watch Intelligence Could Be a Major Accessibility Breakthrough

      September 10, 2026

      Apple May Skip the Base iPhone 18 This Year

      September 10, 2026

      FCC changes robot vacuum rules, potentially affecting future models

      August 8, 2026

      Samsung’s new 2TB 990 SSD drops to its lowest price yet

      August 6, 2026
    • Adobe

      Illustrator Linked Images: Build a Handoff That Opens Correctly on Another Computer

      November 3, 2026

      Lightroom Classic Export Settings: Pixel Dimensions and Print Resolution Explained

      November 2, 2026

      InDesign Text Flow: Use Breaks for Destinations and Paragraph Rules for Relationships

      October 31, 2026

      Premiere Audio Automation: Choose Between Clip and Track Changes

      October 31, 2026

      Photoshop Layer Masks: Fix the Visibility Without Repainting the Photograph

      October 30, 2026
    • Microsoft

      Excel SUBTOTAL: Make Your Total Match the Rows You Mean to Include

      November 3, 2026

      How to Make a Smaller PowerPoint Delivery Copy Without Losing Your Master

      November 2, 2026

      How to Keep Leading Zeros and Long Identifiers Safe in Excel

      November 2, 2026

      Excel XLOOKUP: A Matching Result Does Not Prove a Unique Record

      October 31, 2026

      Word Headers and Footers: Understand the Section Before Editing the Page

      October 30, 2026
    • java

      Java Array Copies: A New Array Does Not Always Mean Independent Objects

      November 3, 2026

      Java Streams Are Single-Use: Rebuild the Pipeline for a Second Result

      November 3, 2026

      Java String Replacement: When to Use Literal Text and When to Use Regex

      November 2, 2026

      Java Periodic Tasks: Choose the Schedule and the Failure Policy Together

      October 31, 2026

      Java Text Files: Choose Between Whole-File and Line-Based Reading

      October 30, 2026
    • Oracle

      Oracle COUNT: Define the Metric Before Choosing the Expression

      November 3, 2026

      Oracle EXISTS: Find Matching Parent Records Without Multiplying Rows

      November 2, 2026

      Oracle LISTAGG: Make Long-List Truncation Visible to the Reader

      October 31, 2026

      Oracle UNION and UNION ALL: Decide Whether Repeated Rows Carry Meaning

      October 30, 2026

      Oracle Sequences: A Missing Number Does Not Prove a Missing Row

      October 30, 2026
    Şevket Ayaksız
    Anasayfa » Java Array Copies: A New Array Does Not Always Mean Independent Objects
    java

    Java Array Copies: A New Array Does Not Always Mean Independent Objects

    By sevketayaksizNovember 3, 2026No Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Java Array Copies: A New Array Does Not Always Mean Independent Objects
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Copying an array can protect its structure without protecting the mutable objects inside it. That distinction matters when code passes data between components and expects one component’s changes to remain isolated.

    The useful question is not simply whether an array was copied. It is which parts of the data now have independent storage and which references are still shared.

    Start with the outer container

    Arrays.copyOf creates an array with the requested length. For an object array, values at corresponding positions are preserved; additional positions are filled with null when the requested length is larger.

    Consider:

    StringBuilder[] original = {
        new StringBuilder("draft")
    };
    
    StringBuilder[] copy =
        Arrays.copyOf(original, original.length);

    The two arrays are separate containers. However, their first elements refer to the same StringBuilder.

    This is a direct consequence of copying reference values. The method has no application-specific instruction to construct a new version of every referenced object.

    Distinguish replacement from mutation

    These operations have different effects:

    copy[0].append("-reviewed");

    This changes the shared builder, so observing original[0] also reveals the changed content.

    By contrast:

    copy[0] = new StringBuilder("approved");

    This replaces a reference in the copied array. It does not replace the reference stored in the original array.

    This small example is a useful review tool because it separates two questions: “Can the caller change an array entry?” and “Can the caller change an object reached through an entry?”

    A defensive copy of the container addresses the first question. It does not automatically answer the second.

    Define copying around the domain

    If independent mutable elements are required, construct them deliberately:

    StringBuilder[] independent =
        new StringBuilder[original.length];
    
    for (int i = 0; i < original.length; i++) {
        independent[i] = new StringBuilder(original[i]);
    }

    This example assumes every element is non-null. Production code must define the intended behavior for null entries rather than accidentally failing on them.

    More complex domain objects require more careful decisions. Should a copied order share its customer record? Should it retain the same identity? Should nested collections also be copied?

    There is no universal answer hidden inside the phrase “deep copy.” The application needs a policy describing which relationships remain shared.

    Test ownership through observable behavior

    A useful test changes the copied container and checks that the original container is unaffected. A second test changes an element and checks whether the original element should or should not change.

    Both tests are necessary when mutable objects are involved.

    Multidimensional arrays deserve similar attention because their elements can themselves be arrays. Copying only the outer structure does not establish independence for every nested level.

    Choose names that communicate intent, such as snapshotValues or copyReferences, when they clarify a public API. Document whether callers may modify returned data and what those modifications affect.

    The strongest protection comes from an explicit ownership model. Array-copying methods are useful building blocks, but the application must decide what independence actually means.

    Sources: Oracle: Arrays API and copyOf

    Views: 8
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    sevketayaksiz
    • Website

    Related Posts

    Java Streams Are Single-Use: Rebuild the Pipeline for a Second Result

    November 3, 2026

    Java String Replacement: When to Use Literal Text and When to Use Regex

    November 2, 2026

    Java Periodic Tasks: Choose the Schedule and the Failure Policy Together

    October 31, 2026
    Add A Comment

    Comments are closed.

    Şevket Ayaksız
    Instagram
    • Home
    • Adobe
    • microsoft
    • java
    • Oracle
    • Contact
    © 2026 Theme Designed by Şevket Ayaksız.

    Type above and press Enter to search. Press Esc to cancel.