Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing arrays

 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class PassA {
public static void main(String [] args) {
PassA p = new PassA();
p.start();
}

void start() {
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3) {
a3[1] = 7;
return a3;
}
}

here's how my reasoning goes:-

a1 array holds values 3,4,5
and the 1st system out line should print out exactly that 3,4,5

now a2 is passed into a method, the method changes a2's values to 3,7,5 and returns the value back to the calling method.
so the 2nd system out line should have printed 3,7,5.
thus I was expecting: 3 4 5 3 7 5 as the prog. output. [but the output is 15 15. ]

what is wrong in my reasoning ?
Thx.
 
Arnab karmakar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Netty,

I got the reson.
If u look closely the entire thing is the magic of pointers.
Though there is no pointer in Java, but whenever an object
is stored in java, it is always stored by reference. And
an array is an object(in Java).

Now what is happening here.

1. First u r storing 3,4,5 in a1
2. Then u are passing the pointer to of a1 to fix.
3. In fix a3 gets the pointer of a1.
4. a3's value is modified.
5. Then u are transfering the pointer of a3(that is a1) to a2.
6. Ultimately, all the values are changed(a1,a2,a3).

I dont know whether I am able to answer u or not.
Reply if u have not got me.

Arnab
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Netty
if you see program again at line
System.out.print(a1[0] + a1[1] + a1[2] + " ");
then you will find

1. values of Array are being summed up.
2. wehn function fix is called a reference of a1 is sent so vaule of array 1 will also change before System.out.print is called

hence 3 + 7 + + 5 = 15

wil be printed twice
 
Vipin Das
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When an array is passed to a method, its reference is being passed. Thus the change made to the array inside the method
will take effect in the original array also. Thus the line a3[1] = 7 changes the value of a1[1] also from 4 to 7.
 
r phipps
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one was a good learning experience.

class PassA {
public static void main(String [] args) {
PassA p = new PassA();
p.start();
}

void start() {
long [] a1 = {3,4,5};
long [] a2 = {3,4,5};
a2 = fix(a2);
System.out.print(a1[0] + ","+ a1[1] + ","+ a1[2] + ", ");
System.out.println(a2[0] +","+ a2[1] + ","+a2[2]);
}

long [] fix(long [] a3) {
a3[1] = 7;
return a3;
}
}
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
firstly, thanks to all for the "," explanation [with and without]

secondly:-"When an array is passed to a method, its reference is being passed. Thus the change made to the array inside the method
will take effect in the original array also"

I think this is then a 'special' case with arrays, where I can take it for granted that they are never passed by value.is my assumption right ?

Thanks
 
Vipin Das
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In java all objects are passed to a method by reference and an array is considered as an object in java.Therefore this is not a special case, it is a general case for all objects. If u pass an object to a method and change the value of its instance variables, the changes will reflect in the original object also.
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If everything gets passed as a reference, in the example below, int y =3 is being passed by value[ so maybe that's a 'but',,...primitives are passed by value ]. so let me distillate by asking , can arrays be passed by value to a method ?

class GFC403 {
private static int x=1;
static void m1(int i) {x++; i++;}
public static void main (String[] args) {
int y=3; m1(y);
System.out.println(x + "," + y);
}}

Thx.
 
Vipin Das
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Primitives are passed by value, but objects are passed by reference. Arrays
are objects, therefore it can only be passed by reference as far as i know.
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vipin

Thanks for the clarification.

*[unless someone else wants to add something ]
 
Vipin Das
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
If you want to send a copy of array to a method and the changes made their must not affect in the original array u can use the clone of the original array and send to the method. This wont work if the array is an array of object references.
example:
[code]

public class scjp {
public static void main(String args[]) {
String s[] = new String[]{"india", "kerala"};
System.out.println(s[0]);
m((String[])s.clone());
System.out.println(s[0]);
//Here s[0] remains the same and prints "india"

}
static void m(String t[]){
t[0] = "vithura";
}
}
[\code]
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx !
afaik clone() is one worry less for the exams...
but nevertheless, the 'value' e.g definitely helps.
 
Nina Binde
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I followed the part where objects are passed by reference and promitives by values. But why would it print 15 in the first instance when it should print out 3 7 5 instead since I am assuming it converts all the longs to String. In the second instance, yes it would print 15.

Thank you!
 
sashi balu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is in response to Nina's post,

class PassA {
public static void main(String [] args) {
PassA p = new PassA();
p.start();
}

void start() {
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.println(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3) {
a3[1] = 7;
return a3;
}
}

The above code prints 15, 15. As per the K & B book, pg 208, the operands are evaluated from left to right and if either of the operand evaluates to a string the operands are concatenated otherwise it will just add the numeric operands. So 15, 15 o/p makes sense.

Sashi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic