Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Arrray?

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the why the following program's output is 4, can anyone explain to me how it works? Since the change_i does not return a value and how could the output is 4?

public class test {
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]) {
i[0] = 2;
i[0] *= 2;
}
}

Thanks!
Mindy
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you're sending an array of integers (which isn't getting changed - the reference is the same when the method returns.) But you can change the array's elements within the method - just as you can change the state of an object whose reference has been passed to a method.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mindy, First, I strongly suggest you read the Campfire Story Pass-By-Value. It gives a very good explanation of how java deals with passing by value and reference.
The first declaration of i[] is not used since you declare it again inside the main method and then pass it to change_i(). As a matter of fact you would have to make the declaration of the first i[] static before you could access the array since it is outside any method or static blocks.
When you passed i to the change_i method you passed the reference to i[]. Now any changes made to i[]'s elements are made to the original version of i[]. In other words a copy of i[] was not sent to change_i. A reference was sent giving change_i() direct access to the original copy of i[] and all its elements.
Hope this helps
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Richard, after reading your explaination and the campfire "pass by value", I totally understand now.

Thanks again!
Mindy
[This message has been edited by Mindy Wu (edited June 12, 2001).]
 
I can't take it! You are too smart for me! Here is the tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!