• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please help regarding collections

 
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added a string object in an ArrayList.
Then i made the string reference point to another string object.
And after that when i iterate through the ArrayList, it still displays the old string object.
I am confused as to how this happens.
Could you kindly explain.

Below is the code

String s = new String("Stevens");

ArrayList<String> list = new ArrayList<String>();

name.add("Aditya");
name.add("Akash");
name.add(2,s);
name.add("Smith");
name.add("Darren");

s = new String("Ronan");

for (String str : list) {
System.out.println(str);
}

Below is the output,

Aditya
Akash
Stevens
Smith
Darren

Should not the list display Ronan , instead of Stevens ???
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohit W. Tawde wrote:I am confused as to how this happens.


It happens because Strings are immutable (Google it) - that is to say: you can't change a String, you can only reassign it with a new different one.

So, while 's' may point to whatever new value you've given it, the element retrieved by name.get() will still point to the old one.

You CAN, however, change an element's value. Have a look at the ArrayList.set() method.

HIH

Winston
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should by now know how to use code tags.

Why do you think that re‑assigning a reference outside the List would change the contents of the List? You do not add the reference s to the List, you add the String "Stevens". By changing the reference s, you do not change the reference inside the List.

And don’t use new String("Ronan"). Use "Ronan".
 
Rohit W. Tawde
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much guys.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohit W. Tawde wrote:Thank you so much guys.


You're most welcome.

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . .
You're most welcome.

Winston

Agree
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You do not add the reference s to the List, you add the String "Stevens". By changing the reference s, you do not change the reference inside the List.


Surely, as your second sentence suggests, you mean that you add a copy of the reference s to the List. Strings, or any other objects for that metter, are never added to Lists.


 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right. Copy of the reference to s, which is not changed when s changes.
 
Rohit W. Tawde
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you add the copy of the reference s to the list, say 'p'.
So if we change the object to which the reference s is pointing, it has no effect on the reference added to the list ie 'p'.
And the only way we could change the object in the list is by name.add(2,"Ronan") OR name.set(2,"Ronan").
I hope i am right.
Kindly confirm it. Please.
Thank you.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohit W. Tawde wrote:So you add the copy of the reference s to the list, say 'p'.


Correct. At least if I understand what you're trying to say.

s is a reference variable. Its value is copied and passed to the add method, which in turn copies it to place it into its list structure, so let's call the list's copy 'p'. So now the list has a reference (p) with the same value as the s variable. Both s and the p point to the same object.

So if we change the object to which the reference s is pointing, it has no effect on the reference added to the list ie 'p'.


Correct. If we do s = something_else; that just puts a different value into s. That is, s now points to a different object, or has the value null, so it doesn't point to any object. That has no effect whatsoever on the list's 'p' reference, or on the object that s previously pointed to.

And the only way we could change the object in the list is by name.add(2,"Ronan") OR name.set(2,"Ronan").


Depends what you mean by "change the object." The phrase is a bit vague. However, if you mean "The only say to change which object that list element points to..." then, yes, adding at that position (or before that position, so it has to shift), or setting at that position, or calling remove() on that position or a preceding one, or clearing the list.

In short, the only way to affect which object a given element of the list refers to is through the list's methods.

Note, however, that if we our original value in our s variable, and then we do something like s.someField = something or s.setSomeField(something), since it's the same object that the list is pointing to, the list will see that change.
 
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic