By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,795 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,795 IT Pros & Developers. It's quick & easy.

When does a method manipulate its arguments?

P: n/a
This method:

static void swap(int a, int b) {
int c = a;
a = b;
b = c;
}

does not swap the values of a and b over. It doesn't work for String
variables either. However the following method does swap the first two
elements of the array a[] over.

static void flip(int[] a) {
int c = a[0];
a[0] = a[1];
a[1] = c;
}

So in the first case the method doesn't appear to manipulate its
arguments whereas in the second case it does.

In typing the above I think I may have worked out the answer to the
question I was going to ask. In the first case memory is temporarily
allocated to the variables a and b whereas in the second case the size
of a[] is not known so memory cannot be allocated. This doesn't quite
explain why String variables are not manipulated but I think there is a
default memory allocation for a String variable. It seems that methods
can manipulate array arguments. So is there a general rule as to whether
a method will manipulate its arguments or not?
Paul

Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Paul wrote:
This method:

static void swap(int a, int b) {
int c = a;
a = b;
b = c;
}

does not swap the values of a and b over. It doesn't work for String
variables either. However the following method does swap the first two
elements of the array a[] over.

static void flip(int[] a) {
int c = a[0];
a[0] = a[1];
a[1] = c;
}

So in the first case the method doesn't appear to manipulate its
arguments whereas in the second case it does.

In typing the above I think I may have worked out the answer to the
question I was going to ask. In the first case memory is temporarily
allocated to the variables a and b whereas in the second case the size
of a[] is not known so memory cannot be allocated. This doesn't quite
explain why String variables are not manipulated but I think there is a
default memory allocation for a String variable. It seems that methods
can manipulate array arguments. So is there a general rule as to whether
a method will manipulate its arguments or not?
Paul

Its because arrays are passed by reference. This means that called
function that recieves a variable passed by reference is actually
manipulating the same area of memory as the original calling function.

Strings, ints, etc are passed by value. The called function only
recieves a copy of the passed variable.

Jul 17 '05 #2

P: n/a
Bob Sill wrote:
Paul wrote:
This method:

static void swap(int a, int b) {
int c = a;
a = b;
b = c;
}

does not swap the values of a and b over. It doesn't work for String
variables either. However the following method does swap the first two
elements of the array a[] over.

static void flip(int[] a) {
int c = a[0];
a[0] = a[1];
a[1] = c;
}

So in the first case the method doesn't appear to manipulate its
arguments whereas in the second case it does.

In typing the above I think I may have worked out the answer to the
question I was going to ask. In the first case memory is temporarily
allocated to the variables a and b whereas in the second case the size
of a[] is not known so memory cannot be allocated. This doesn't quite
explain why String variables are not manipulated but I think there is
a default memory allocation for a String variable. It seems that
methods can manipulate array arguments. So is there a general rule as
to whether a method will manipulate its arguments or not?
Paul

Its because arrays are passed by reference. This means that called
function that recieves a variable passed by reference is actually
manipulating the same area of memory as the original calling function.

Strings, ints, etc are passed by value. The called function only
recieves a copy of the passed variable.


Thanks for that. So are objects passed by reference too? I'm being lazy
because I could write a piece of code to find out. The reason I'm a
little confused is because I thought a String was an object rather than
a primitive.

Jul 17 '05 #3

P: n/a
Paul wrote:
Bob Sill wrote:
Paul wrote:
This method:

static void swap(int a, int b) {
int c = a;
a = b;
b = c;
}

does not swap the values of a and b over. It doesn't work for String
variables either. However the following method does swap the first
two elements of the array a[] over.

static void flip(int[] a) {
int c = a[0];
a[0] = a[1];
a[1] = c;
}

So in the first case the method doesn't appear to manipulate its
arguments whereas in the second case it does.

In typing the above I think I may have worked out the answer to the
question I was going to ask. In the first case memory is temporarily
allocated to the variables a and b whereas in the second case the
size of a[] is not known so memory cannot be allocated. This doesn't
quite explain why String variables are not manipulated but I think
there is a default memory allocation for a String variable. It seems
that methods can manipulate array arguments. So is there a general
rule as to whether a method will manipulate its arguments or not?
Paul

Its because arrays are passed by reference. This means that called
function that recieves a variable passed by reference is actually
manipulating the same area of memory as the original calling function.

Strings, ints, etc are passed by value. The called function only
recieves a copy of the passed variable.


Thanks for that. So are objects passed by reference too? I'm being lazy
because I could write a piece of code to find out. The reason I'm a
little confused is because I thought a String was an object rather than
a primitive.

Yes, objects are passed by reference; that is a fundamental concept in
Java. Perhaps it would be adviseable to take a look at some of the
beginning Java tutorials to pick up the 'quirky' differences between
Java and whatever language you're coming from.

Jul 17 '05 #4

P: n/a
Bob Sill wrote:

Strings, ints, etc are passed by value. The called function only
recieves a copy of the passed variable.

Hi What do you mean by 'etc' ?
int's are primitives, and Strings are objects
So what common is between Strings and int's that they are both passed by
value?

Regards

maciek

Jul 17 '05 #5

P: n/a
"Maciek" == Maciek Zywno <zy***@stud.ics.p.lodz.pl> writes:

Maciek> Bob Sill wrote:
Strings, ints, etc are passed by value. The called function
only recieves a copy of the passed variable.


Maciek> Hi What do you mean by 'etc' ? int's are primitives, and
Maciek> Strings are objects So what common is between Strings and
Maciek> int's that they are both passed by value?

A reference is a primitive type. It is passed by value. In addition,
String objects are immutable (read-only). The end result is that
passing a String type to a function is, for all practical purposes,
just as if you had passed it by value since you can't change the
reference (it is passed by value) and you can't change the object (it
is immutable).

If you find this a little mind bending at first glance, that is only
because it is ;-)

Cheers!
Shyamal
Jul 17 '05 #6

P: n/a
Shyamal Prasad wrote:
"Maciek" == Maciek Zywno <zy***@stud.ics.p.lodz.pl> writes:

Maciek> Bob Sill wrote:
>> Strings, ints, etc are passed by value. The called function
>> only recieves a copy of the passed variable.


Maciek> Hi What do you mean by 'etc' ? int's are primitives, and
Maciek> Strings are objects So what common is between Strings and
Maciek> int's that they are both passed by value?

A reference is a primitive type. It is passed by value. In addition,
String objects are immutable (read-only). The end result is that
passing a String type to a function is, for all practical purposes,
just as if you had passed it by value since you can't change the
reference (it is passed by value) and you can't change the object (it
is immutable).


Thanks everyone for your help. Reading the above I thought that my swap
method would work for StringBuffer objects, but it doesn't. I think I
now understand it. I could swap array elements over because a single
reference was being passed but when I try to swap objects such as
Strings and StringBuffers I am trying to swap two references, which
doesn't work because the references are not passed back. Please correct
me if I'm wrong; if I'm not my understanding of Java has improved a lot.
Thanks.

Paul

Jul 17 '05 #7

P: n/a
"Paul" <pa***********@blueyonder.couk> wrote in message
news:Ab****************@news-binary.blueyonder.co.uk...
Shyamal Prasad wrote:
"Maciek" == Maciek Zywno <zy***@stud.ics.p.lodz.pl> writes:

Maciek> Bob Sill wrote:
>> Strings, ints, etc are passed by value. The called function
>> only recieves a copy of the passed variable.


Maciek> Hi What do you mean by 'etc' ? int's are primitives, and
Maciek> Strings are objects So what common is between Strings and
Maciek> int's that they are both passed by value?

A reference is a primitive type. It is passed by value. In addition,
String objects are immutable (read-only). The end result is that
passing a String type to a function is, for all practical purposes,
just as if you had passed it by value since you can't change the
reference (it is passed by value) and you can't change the object (it
is immutable).


Thanks everyone for your help. Reading the above I thought that my swap
method would work for StringBuffer objects, but it doesn't. I think I
now understand it. I could swap array elements over because a single
reference was being passed but when I try to swap objects such as
Strings and StringBuffers I am trying to swap two references, which
doesn't work because the references are not passed back. Please correct
me if I'm wrong; if I'm not my understanding of Java has improved a lot.
Thanks.

Paul


As StringBuffers do not swap the ref, you could still modify the contents of
the object, something you cannot do with a string. So.. you could implement
a swap with StringBuffers :PP

public void swap(StringBuffer a, StringBuffer b)
{
StringBuffer c = new StringBuffer(a.toString());
a.delete(0, a.length());
a.append(b.toString());
b.delete(0, b.length());
b.append(c.toString());
}
Jul 17 '05 #8

P: n/a

"Bob Sill" <wh*@why.net> wrote in message
news:GR***************@bignews6.bellsouth.net...
Paul wrote:
This method:

static void swap(int a, int b) {
int c = a;
a = b;
b = c;
}

(snip)
Its because arrays are passed by reference. This means that called
function that recieves a variable passed by reference is actually
manipulating the same area of memory as the original calling function.

Strings, ints, etc are passed by value. The called function only
recieves a copy of the passed variable.


No, arrays are not passed by reference, array references are passed by
value, just as object references are.

What is the difference?

Changes to the array reference variable or object reference variable can be
made in the originating method, but not in the called method. Changes to
the array or object referenced can be made in either case, assuming that the
class allows them.

The String class provides no methods that allow modification of the String
object. (It does supply ones that will create a new String object,
describing a modified String.)

You can create and swap arrays, such as:

int a[], b[], c[];
a=new int[10];
b=new int[10];
a[0]=1;
b[0]=2;
c=a;
a=b;
b=c;

If you attempt to swap the a and b array references in a called method, it
only swaps copies of the reference variable.

Consider:

static void flip(int[] a, int[] b) {
int [] c;
// swap array reference variables
c=a;
a=b;
b=c;
// assign array elements using swapped reference variables
a[0] = a[1];
b[1] = b[0];
}

Since the copies of the array references were swapped, the original b[1] is
assigned to a[0], and the original a[0] to b[1].

Note that this all works exactly the same as pointer variables do in C,
except that it is not possible to have an array reference variable that
"points" other than to the beginning of the array.

-- glen


Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.