• 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

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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ahmet oguz
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic