473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

When does a method manipulate its arguments?

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
8 2539
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
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
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
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
"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
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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
13
by: a1eah71 | last post by:
I have an application where the user loads graphics from another site. Presently I advise the user to wait for graphics to load before clicking on adjustments. Ideally, I would prefer a closed-loop...
32
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
1
by: David Veeneman | last post by:
Does anyone knmow why an asynchronous worker method would crash if the e.Cancel method is set to true, but not otherwise? I'm working on a demo app with an asynchronous call, using the .NET 2.0...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
0
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it tries to get it calls this Profile property ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.