Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:43:29+00:00 2026-06-14T20:43:29+00:00

I am a little confused as to how my Java program is allocating memory.

  • 0

I am a little confused as to how my Java program is allocating memory. Below is a typical situation in my program. I have an object created on the Heap inside of a class which I pass to other functions inside of other classes.

public class A {
    private List<Integer> list = new ArrayList<Integer>();

    public A() {
        for(int i = 0; i < 100; i++)
            list.add(i);
    }

    public void foo() {
        B b = new B();
        b.bar(this.list);
    }
}

public class B {
    A a = new A();

    public int bar(List<Integer> list) {
        list.add(-1);
        return list.size();
    }

    public int getSize() {
        List<Integer> list = a.list;
        return a.size();
    }
}

My question(s) are about how the memory is used. When A’s list gets passed to B.bar(), does the memory get copied? Java is pass-by-value so I assumed the B.bar() function now contains a copy of the list which is also allocated on the heap? Or does it duplicate the list and put it on the stack? Or is the list duplicated at all? Similarly what applies to these memory questions inside of B.getSize()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:43:30+00:00Added an answer on June 14, 2026 at 8:43 pm

    It’s probably clearer to say that in Java, object references are passed by value.

    So B gets a copy of the reference (which is a value) to the List, but not a copy of the List itself.

    You can easily prove this to yourself by getting B to modify the List (by adding or removing something), and observing that A’s List also changes (i.e. it is the same List).

    However, if you change B’s reference to the List (by creating and assigning a new List to it), A’s reference is unchanged.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a little confused of the handling between the java Date Class and GregorianCalendar
I am a little bit confused on exactly how to program this. I have
I'm a little confused about iterators in Java. How do I make next() only
I am a little confused about the roles of a java application server and
I am trying to profile my java application with JProbe. I am little confused
I am a little confused about Accept-Encoding . I have Web Service which would
I'm a little confused by the structure of the Node class in OpenJDK's implementation
I am little confused about Java frameworks (I am coming from .NET). Do you
I am a little confused with Java All passings by value concept. Consider the
I'm learning C# and coming from a Java world, I was a little confused

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.