This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes why don't we change the arraylist element by reference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why don Watch "why don New topic
Author

why don't we change the arraylist element by reference?

ahmet oguz
Greenhorn

Joined: Mar 08, 2006
Posts: 18
Hi,
in java, primitives types move with pass by value but other types(Object Types) with pass by value of their references. There is a question at this point.
if we have got an arraylist with element of strings why don't we change the arraylist elements? a sample code:
ArrayList a = new ArrayList();
a.add("istabul");
a.add("Ankara");
a.add("Denizli");

if we take a new arraylist we pass the value of reference to the k variable.
ArrayList k = a;
This is ok now k and a is the same reference to the arraylist

but if we take the element of arraylist which elements are String
why don't we chage elements of arraylist?
String string = (String)a.get(2); --this is "Denizli"
string = "İzmir";
now the arraylist third element at position 2 is Denizli Why? I don't understand this point. String is pass by value of references but this is not ok
Thanks.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

ilter wrote:
ArrayList a = new ArrayList();
a.add("istabul");
a.add("Ankara");
a.add("Denizli");
String string = (String)a.get(2); --this is "Denizli"
string = "İzmir";

now the arraylist third element at position 2 is Denizli Why? I don't understand this point. String is pass by value of references but this is not ok


You create a String object with the content "Denizli" at the line where you add it to the ArrayList. The ArrayList variable a refers to it (and to the other turkish towns).
In the line --this is "Denizli" a String variable named string now also refers to the "Denizli" - String-object within the ArrayList.
In the last line, the variable string now refers to (~ points to) a new object ("İzmir"). But this will not change the ArrayList. It's like this:


Denizli is still at its place in the ArrayList.
The method get(int i) does not remove an alement, it only returns a reference to it. Using the get-method does not change a list.

Yours,
Bu.


all events occur in real time
ahmet oguz
Greenhorn

Joined: Mar 08, 2006
Posts: 18
hi, thanks but my mind went blank.

when i run this java code the result is:
denizli
10-ankara

why public static void change(String s) function does not change the value
but
public static void change(Deneme s) fucntion changes the value of string.

I know that Objects pass by value of their references but the change function that parameter type is string does not change the string.
Please help me
thanks
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Strings never get changed - they are immutable, their value is fixed.

s.s = "ankara";

causes the String reference in the object referenced by s to refer to the new String "ankara". That change to the *reference* is seen by everyone using that object.

s = "ankara";

causes the *local* reference s to refer to the new String "ankara". As the reference is local, that change is not seen from code outside the method.

Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Arnav Velimala
Ranch Hand

Joined: Jun 04, 2007
Posts: 37
In Java, it is pass-by-value.

String k = "denizli";
change(k);

When you invoke change(k) method, a *copy* of 'k' is created and is passed to the method. Not the original 'k' is passed(since it is pass-by-value). So any changes made in the method will be reflected in the copy-variable and not the original value. That's why change(String s) method does not change the value.

References are also passed-by-value - a copy of reference will be created(the created copy will now be an alias to the object the original reference is refering to).

Deneme d = new Deneme();
change(d);

public static void change(Deneme s){
s.x = 10;
s.s = "ankara";
}

d & s are aliases to the Deneme object. With 's' reference you can change the *state* of the object as 'd' and 's' denotes the same object.

Since Java passes by-value,

Deneme d = new Deneme();
change(d);

public static void change(Deneme s){
s=null
}

will not nullify the "d" reference. The statement System.out.println(d.x + "-" + d.s); will give NullPointerException IF it has been nullified.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why don't we change the arraylist element by reference?
 
Similar Threads
Sun Cirtification
Pass By Reference
How to delete an index.
wrapper classes
Head First Java Five Minute Mystery p. 268