• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

objects passed by reference

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I'm a bit confused by the following code:
[code]
public class TestClass {
static String str = new String ("1");
static int[] iArr = {1};

public static void changeIt(String s) {
s = "2";
}

public static void incr(int[] n) {
n[0]++;
}

public static void main(String[] args) {
changeIt(str);
System.out.println(str);
incr(iArr);
System.out.println( iArr [ 0 ] ) ;
}
}
[code]

Why this program behaves differently for the array and the string?
I would expect it printing

2
2
because references are passed to the method and then the object to which they point to is changed by the method

But it prints :
1
2

Why?

What is the difference between the objects passed in the two method calls ?

thank you,
Gianni
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A copy of the reference str is passed to the method changeIt, both the copy and origonal refence , reference the object created by new String ("1"), in change it the copy of the reference is made to reference a different string object "2", On exit from changeit the copy of the refence to "2" is gone, and all you have is the origonal reference still "1".

In the second call incr (iArr) you again create a copy of the reference in this case iArr BUT you use the refence to alter the referant, as both the origonal reference and the copy refer to the same array , even after the copy of the refernce is gone on return from incr the change is visible.

This might help (change your changeIt method to this)



The results are ...

true
false
2 1
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects are not passed by reference. You pass a copy of the objects reference to the method called.

G
[ April 02, 2007: Message edited by: Gavin Tranter ]
 
gianni ipez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much,
I think I got it.
 
gianni ipez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much,
I think I got it.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Array is also an object -- Isn't it?
So, why this anomaly?
Please help.

Thanks.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What anomaly do you mean?

Array references are object references and are sent by value like other references.
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic