• 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

Call by reference not working

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

if you declare and initialize an array in main and give it as a parameter over to another function 'reverse' and make it point there to another array... why does it still point to the original array once this function 'reverse' has finished?

Below is the code.

public static void main( String[] args )
{
double[] array = {1,2,3,4,5,6,7};
printArray( array );
reverse(array);
printArray( array );
}

public static void reverse( double[] array ) {
if ( array == null )
return;
double[] newArray = new double[array.length];
int i=array.length-1;
for(double item:array) {
newArray[i--]=item;
}
array=newArray;
}

public static void printArray(double[] array) {
for(double item: array) {
System.out.print(item + ", ");
}
System.out.println();
}

}

Output:

1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,

I'm giving the array variable which is declared in main over to another function (printArray).
There I'm making it point to another array declared there. When I'm back in main and print the contents of the array, it prints 1, 2, ...,7.

I'm not getting this. I would understand, if it would print nonsense stuff, because the newArray was declared in reverse and when reverse is finished, then newArray 'doesnt exist anymore'...so array would point to some non-defined memory-area.... but it prints 1, 2, ... , 7.... the values it was initialized with before reverse was called.

I know, the answer is probably very simple but I need some help right now...

Thanks in advance.

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Java never passes by reference, it only uses pass by value
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, ok... now I'm getting it...

You pass the reference as a 'value'... that means you can change the object the reference is referring to, but can't make the reference point to some other object...

If you write something like array[0]=123.456d in reverse the change can be seen in main as well...

Thanks...

(and I still have quite much to learn...)
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you got it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic