0

I come from C++ with pointers and references ( const string& str / string* str ).

I learnt that you create an object in Java via the new statement.

Bucket b1 = new Bucket();

So now I have a reference called b1 which "points" to an object of type Bucket, right?

Well in my android application I handle a lot with lists. These lists might have 1000 objects inside.

for( int i = 0; i < 1000; i++ )
   myList.add( new Bucket() );

My list now, contains 1000 Bucket objects. Or does it contain 1000 references to Bucket objects?

But if i add another item to the list like this :

b1 = new Bucket();
myList.add( b1 );

do b1 and myList refer to/ contain the same object (of course, myList at its last index )?

And if I have another list :

List<Bucket> anotherList = myList;

do both lists refer to or contain the same objects? Or does anotherList refer to myList?

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
Davlog
  • 2,162
  • 8
  • 36
  • 60

4 Answers4

3

Java is always pass by value. However, the value of any Object instance is a reference address.

In your question anotherList isn't another List. It's a reference to myList,

List<Bucket> anotherList = new ArrayList<>(myList);

That's another instance of List.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • so any changes to anotherList will cause the same change to myList (in my question)? – Davlog Jul 01 '14 at 13:30
  • ` the value of any Object instance is a reference address. That is why the default toString() looks like an address`...what does `toString()` has to do with reference address? The value printed in default `toString()` is the hashcode of the object. – Yasin Jul 01 '14 at 13:31
  • @Yasin Edited. Because it's irrelevant for the answer, but then why is "a" != new String("a") – Elliott Frisch Jul 01 '14 at 13:35
  • @Yasin [Documentation](http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode%28%29): _Returns a hash code value for the object. [...]_ ***This is typically implemented by converting the internal address of the object into an integer*** _[...]_ In most cases it _is_ the memory address. – BackSlash Jul 01 '14 at 13:35
  • @ElliottFrisch The latter explicitly creates a new and referentially distinct instance of a String object; the former may reuse an instance from the string pool if one is available. – Yasin Jul 02 '14 at 09:18
2

Java works with address references.

For example, in your case, if you change myList you change anotherList to.

To have two diferent lists with the same info you need to do something like this:

List<String> a = new ArrayList<String>();
List<String> b = new ArrayList<String>();
Collections.copy(b, a);

In this case you can change list a without change list b.

João Marcos
  • 3,872
  • 1
  • 19
  • 14
1

So now I have a reference called b1 which "points" to an object of type Bucket, right? Right

My list now, contains 1000 Bucket objects. Or does it contain 1000 references to Bucket objects? 1000 references

do b1 and myList refer to/ contain the same object Yes the same

do both lists refer to or contain the same objects? Or does anotherList refer to myList? There is one list only - both anotherList and myList refer to it

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
1

As you are not creating any object for anotherList .It will refer to myList. Ex:

List<> x = new ArrayList<>();

List<> y =x;

Here y will have the reference to x.

Nadendla
  • 712
  • 2
  • 7
  • 17