Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equalization two complicated objects in java (two objects that are made from other object)

Let's say you have this piece of code in java: Contact is made from name and number, and PhoneBook from an array of Contacts.

//set -> Does this method really copies and creating new memory place or just pointing to the memory?

public void setContact(Contact[] contact)
{
    this.contact =  contact; // <----this
}

Thanks.


1 Answers

Arrays are objects, and object references are passed by value in Java. So calling this method makes this.contact be a copy of the reference to the contact array passed as argument. No copy of the array elements is made. No copy of the array is made.

like image 199
JB Nizet Avatar answered Mar 12 '26 18:03

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!