Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array  RSS feed

 
Bhaswati Karmakar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1) What happens when the following program is compiled and run. Select the one correct answer.
public class example {
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;
}
}
Answer: The program prints 4.

Q2) What happens when the following program is compiled and run. Select the one correct answer.
public class example {
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[]) {
int j[] = {2};
i = j;
}
}
Answer: The program prints 1.

Could you please explain:

Why does the value of "i[0]" change in Q1 and does not change in Q2 ?
 
js yang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you pass an object( such as an array ) to a method, you are passing a copy of the reference. Any changes to the object( such as changing the value i[0] = 2 ) changes the actual object. Thus, in your first example, i[0] actually contains the value of 4.
However, you have to keep in mind that you are still passing only a copy of the reference. If you point the passed reference to another object, you can no longer affect the actual object. In other words, in your second example, the expression i = j does not affect the actual array i[]. It just points the copied reference to another object( in this case, to the array j ).
For example, look what happens when you change the change_i method:
public static void change_i(int i[]) {
int j[] = {2};
i[0] = 5;
i = j;
i[0] = 10;
}
i will print out 5. The expression i[0] = 10 does not affect array i because the reference is pointing to the array j.
I hope this helps a little bit--I still find this issue a little baffling. However, playing around with code helped me.
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see it the same as Js.
Just to sum it up. When you pass an object, you are actually passing an address or reference to the object.
When an element or member of the object is changed, the reference to the object stays the same; therefore, when you change a member or element of an object, the reference does not change. As a result, we are able to change members or elements of objects, and the computer still thinks that since the reference was not changed.
However, the reference will be changed when it is assigned a new object.

I hope that helps.
 
Bhaswati Karmakar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you for the explanation.
JS,
In your example,
For example, look what happens when you change the change_i method:
public static void change_i(int i[]) {
int j[] = {2};
i[0] = 5;
i = j;
i[0] = 10;
}
i will print out 5. The expression i[0] = 10 does not affect array i because the reference is pointing to the array j.
Does that mean j[0]=10 ?
 
js yang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i[0] is 5, and j[10] is 10.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!