• 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

Arrays

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. First
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;
}
}

2. Second
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;
}
}
Can u tell me the output for these two ?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get 1 and 4.
 
Nitin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but the arrays are passed by reference. The 2nd one is correct as i m passing array in the method and then changing the values in the array. But what about the first one ? there also, i m passing and array and then saying that array i is equal to array j, meaning array i will point to the memory location pointed by j. so array i should now have value 2. pls correct me if i m wrong.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java passes the array reference by value. You cannot change the passed reference; that is, make the reference refer to something else.
You can only change the state of what the reference refers to (the array).

In your first example, the i in the main method will not be changed after the call to change_i. The local i in change_i was changed, but that does not affect the i in main method.

To avoid confusion it can be of benefit to use a different name for your formal argument to the method change_i.
[ November 01, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
* In method change_i, you are creating another array i. Value of that array i is pointing to same array i in main method.

* Another array j is created in change_i.
* Array i in method change_i is pointing to array j. Here reference of array i in change_i method is changed. Still array i in main is pointing to same value.

You can try this:
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};
System.out.println(i[0]);
i = j;
System.out.println(i[0]);
}
}
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry,
For you explanation, try this:


(Code tags added and formatted for readability)
[ November 01, 2004: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing wrong there. That's a change to the referred to object, not to the reference.
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry,
When you run this program, you get text "i in main = 100" This means, array i in main is changed in method. So, let me know still you are thinking arrays are passed by value? and if yes, please explain me.
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Text in sun documentation:

http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html

"Objects and arrays are also passed by value, but the value of an object is a reference. So the effect is that arguments of reference types are passed in by reference."
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not say arrays are passed by value. I said array references are passed by value. It's the reference that is effectively copied into the local parameter defined by the formal parameter of the method.

Changing the local parameter to refer to another array is possible but that does affect the array reference passed to the method. It's of course possible to change the array contents through the reference.
[ November 01, 2004: Message edited by: Barry Gaunt ]
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Barry.
 
reply
    Bookmark Topic Watch Topic
  • New Topic