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.
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.
all events occur in real time
Joined: Mar 08, 2006
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
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
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).