problems sending array as function argument

  • Thread starter flintridgeparkenfarker vonkerschnauzerheiden III \
  • Start date
F

flintridgeparkenfarker vonkerschnauzerheiden III \

The calling function is in a class which imports another class and calls a
function from the imported class. The calling function sends an array of
file objects as a parameter. Attempting to access an element of the array
causes a multitude of errors.

Passing strings and such work okay, but passing by reference [I guess]
doesn't make it through the wash. Am I going to have to physically move the
imported functions into the calling functions module?

Thanks for any input.

joe
 
R

Ryan Stewart

flintridgeparkenfarker vonkerschnauzerheiden III (joe) said:
The calling function is in a class which imports another class and calls a
function from the imported class. The calling function sends an array of
file objects as a parameter. Attempting to access an element of the array
causes a multitude of errors.

Passing strings and such work okay, but passing by reference [I guess]
doesn't make it through the wash. Am I going to have to physically move the
imported functions into the calling functions module?

Thanks for any input.

joe
More details? Code maybe? And if you're passing strings, you're passing by
reference.
 
V

Virgil Green

Ryan Stewart said:
flintridgeparkenfarker vonkerschnauzerheiden III (joe) said:
The calling function is in a class which imports another class and calls a
function from the imported class. The calling function sends an array of
file objects as a parameter. Attempting to access an element of the array
causes a multitude of errors.

Passing strings and such work okay, but passing by reference [I guess]
doesn't make it through the wash. Am I going to have to physically move the
imported functions into the calling functions module?

Thanks for any input.

joe
More details? Code maybe? And if you're passing strings, you're passing by
reference.

Unless it's not Java, you're passing by value.

- Virgil
 
F

flintridgeparkenfarker vonkerschnauzerheiden III \

: "flintridgeparkenfarker vonkerschnauzerheiden III (joe)" <[email protected]>
: wrote in message : > The calling function is in a class which imports another class and calls
a
: > function from the imported class. The calling function sends an array of
: > file objects as a parameter. Attempting to access an element of the
array
: > causes a multitude of errors.
: >
: > Passing strings and such work okay, but passing by reference [I guess]
: > doesn't make it through the wash. Am I going to have to physically move
: the
: > imported functions into the calling functions module?
: >
: > Thanks for any input.
: >
: > joe
: >
: > --
: > "To be is to do."--Plato.
: > "To do is to be."--Socrates.
: > "Do be do be do."--Sinatra.
: >
: >
: More details? Code maybe? And if you're passing strings, you're passing by
: reference.
:
:

private static functionOne() {
File filearray = new File[100];
File f = openFile().getAbsoluteFile(); // get file from dialog
filearray[idx++] = f;
functionTwo(filearray);
}

private static functionTwo(File[] x) {
System.out.println(x[0].getName());
}

Of course there is more to the functions involved, but this is basically the
code. BTW, I put everything within the same class and it didn't make any
difference.

You know what? While I'm parsing this code to post here, I now see what I've
been going wrong. I've been letting the array go out of scope before sending
it [not shown here].

Thanks for your response though. I appreciate it.


joe
 
F

flintridgeparkenfarker vonkerschnauzerheiden III \

: : > "flintridgeparkenfarker vonkerschnauzerheiden III (joe)"
<[email protected]>
: > wrote in message : > > The calling function is in a class which imports another class and
calls
: a
: > > function from the imported class. The calling function sends an array
of
: > > file objects as a parameter. Attempting to access an element of the
: array
: > > causes a multitude of errors.
: > >
: > > Passing strings and such work okay, but passing by reference [I guess]
: > > doesn't make it through the wash. Am I going to have to physically
move
: > the
: > > imported functions into the calling functions module?
: > >
: > > Thanks for any input.
: > >
: > > joe
: > >
: > > --
: > > "To be is to do."--Plato.
: > > "To do is to be."--Socrates.
: > > "Do be do be do."--Sinatra.
: > >
: > >
: > More details? Code maybe? And if you're passing strings, you're passing
by
: > reference.
:
: Unless it's not Java, you're passing by value.
:
: - Virgil
:
:

Thanks for your response. I see what I've been doing wrong though.
[oversight]

Although, to address one point, when I pass an array to another function I
can alter the array in the receiving function and those changes will be
reflected in the calling function's array, just like c++. I realize Java
passes arguments by value but it looks like it may handle arrays the same as
c++, by passing the address to the first element. Of course I'm only
presuming here.

joe
 
R

Real Gagnon

: > > "To be is to do."--Plato.
: > > "To do is to be."--Socrates.
: > > "Do be do be do."--Sinatra.

Interesting but mine is a little bit different,

To be or not to be - Shakespeare
To do is to be. - Kant
To be is to do. - Sartre
Do be do be do. - Sinatra
Yabba dabba do! - Fred Flintstone

Bye.
 
T

Thomas G. Marshall

Virgil Green said:
Ryan Stewart said:
"flintridgeparkenfarker vonkerschnauzerheiden III (joe)"
The calling function is in a class which imports another class and
calls a function from the imported class. The calling function
sends an array of file objects as a parameter. Attempting to
access an element of the array causes a multitude of errors.

Passing strings and such work okay, but passing by reference [I
guess] doesn't make it through the wash. Am I going to have to
physically move the imported functions into the calling functions
module?

Thanks for any input.

joe

--
"To be is to do."--Plato.
"To do is to be."--Socrates.
"Do be do be do."--Sinatra.


More details? Code maybe? And if you're passing strings, you're
passing by reference.

Unless it's not Java, you're passing by value.

- Virgil

Thanks for your response. I see what I've been doing wrong though.
[oversight]

Although, to address one point, when I pass an array to another
function I can alter the array in the receiving function and those
changes will be reflected in the calling function's array, just like
c++. I realize Java passes arguments by value but it looks like it
may handle arrays the same as c++, by passing the address to the
first element. Of course I'm only presuming here.


No, it's passing the reference of the array object (as a value). It can
look much like what happens in C, but it is very different.

Java arrays are confusing for people with C/C++ backgrounds because it is
hard for them to grok the notion of Java arrays as fully blown objects.

For instance, when you create an array of 3 objects, you are creating 4
objects: one for the array itself, and 3 more for each of the objects.

Arrays of primitives are a little more like C's here since all you need do
is create the array object and you get all the primitives, since primitives
need no object-like allocation, but you still need to allocate once for the
array object itself.

When you send the array, you are not sending a pointer to the first element.
You are sending a pointer to the array object itself.
 
T

Thomas G. Marshall

Ryan Stewart <[email protected]> horrified us with:

....[snipitty doo dah]...
More details? Code maybe? And if you're passing strings, you're
passing by reference.

This is a conversation that keeps raising its head up out of the lagoon to
scare the natives.

Java is pass by value /only/.

When you pass an object, you are passing a reference to the object /as a
value/.
 
F

flintridgeparkenfarker vonkerschnauzerheiden III \

: >: > > "To be is to do."--Plato.
: >: > > "To do is to be."--Socrates.
: >: > > "Do be do be do."--Sinatra.
:
: Interesting but mine is a little bit different,
:
: To be or not to be - Shakespeare
: To do is to be. - Kant
: To be is to do. - Sartre
: Do be do be do. - Sinatra
: Yabba dabba do! - Fred Flintstone

Yeah, I like yours. I may just have to steal it. :)

:
: Bye.
: --
: Real Gagnon from Quebec, Canada
: * Looking for Java or PB snippets ? Visit Real's How-to
: * http://www.rgagnon.com/howto.html
 
F

flintridgeparkenfarker vonkerschnauzerheiden III \

"Thomas G. Marshall" <[email protected]>
wrote in message : flintridgeparkenfarker vonkerschnauzerheiden III (joe) <[email protected]>
: horrified us with:
:
: > : >> : >>> "flintridgeparkenfarker vonkerschnauzerheiden III (joe)"
: >>> : >>>> The calling function is in a class which imports another class and
: >>>> calls a function from the imported class. The calling function
: >>>> sends an array of file objects as a parameter. Attempting to
: >>>> access an element of the array causes a multitude of errors.
: >>>>
: >>>> Passing strings and such work okay, but passing by reference [I
: >>>> guess] doesn't make it through the wash. Am I going to have to
: >>>> physically move the imported functions into the calling functions
: >>>> module?
: >>>>
: >>>> Thanks for any input.
: >>>>
: >>>> joe
: >>>>
: >>>> --
: >>>> "To be is to do."--Plato.
: >>>> "To do is to be."--Socrates.
: >>>> "Do be do be do."--Sinatra.
: >>>>
: >>>>
: >>> More details? Code maybe? And if you're passing strings, you're
: >>> passing by reference.
: >>
: >> Unless it's not Java, you're passing by value.
: >>
: >> - Virgil
: >>
: >>
: >
: > Thanks for your response. I see what I've been doing wrong though.
: > [oversight]
: >
: > Although, to address one point, when I pass an array to another
: > function I can alter the array in the receiving function and those
: > changes will be reflected in the calling function's array, just like
: > c++. I realize Java passes arguments by value but it looks like it
: > may handle arrays the same as c++, by passing the address to the
: > first element. Of course I'm only presuming here.
:
:
: No, it's passing the reference of the array object (as a value). It can
: look much like what happens in C, but it is very different.
:
: Java arrays are confusing for people with C/C++ backgrounds because it is
: hard for them to grok the notion of Java arrays as fully blown objects.
:
: For instance, when you create an array of 3 objects, you are creating 4
: objects: one for the array itself, and 3 more for each of the objects.
:
: Arrays of primitives are a little more like C's here since all you need do
: is create the array object and you get all the primitives, since
primitives
: need no object-like allocation, but you still need to allocate once for
the
: array object itself.
:
: When you send the array, you are not sending a pointer to the first
element.
: You are sending a pointer to the array object itself.
:
:
I admit being confused over this issue. In my mind, if I can change the
value of a variable/object in a calling function via a parameter, then it
seems like I have a reference to a memory location. I can't help wondering
if this isn't an exercise in semantics. Either way, it doesn't matter--I
can't get this thing to work anyway.

I'm just fooling with the file compression utility and whenever I attempt to
access anything other than the local directory or a sub, I get "Access
Denied" errors. If the sub directory has a sub, "Access Denied."

I read something about this issue but the statement was brief and I don't
know the context in which it was written.
 
P

Peter Kirk

I admit being confused over this issue. In my mind, if I can change the
value of a variable/object in a calling function via a parameter, then it
seems like I have a reference to a memory location. I can't help wondering
if this isn't an exercise in semantics.

I think you can look at it like this:

You *do* have a reference to a memory location (a reference to an object),
but in your method you can't change the value of that reference - ie you
can't change it so it refers to another object. Well, you can, but that
change won't be maintained when you return from your method.

In your method you can change the "state" of the object being referred to -
ie you can call its methods, set its members (according to what is possible
for that object of course), and these changes will be maintained when your
method returns.

Peter
 
J

John C. Bollinger

flintridgeparkenfarker vonkerschnauzerheiden III (joe) wrote:

[...]
Although, to address one point, when I pass an array to another function I
can alter the array in the receiving function and those changes will be
reflected in the calling function's array, just like c++. I realize Java
passes arguments by value but it looks like it may handle arrays the same as
c++, by passing the address to the first element. Of course I'm only
presuming here.

You have the effect correct, but the details muddled. Java arrays are
full-fledged objects, and as such they are manipulated via references.
When you pass an array (or any other object) as a method argument you
are passing a reference to it by value. The receiving method then has a
reference to the same object that the invoking method is using, and thus
modifications performed on one side are visible on the other.

Java references should not be equated with C/C++ pointers. Most
particularly there is no pointer arithmetic in Java.


John Bollinger
(e-mail address removed)
 
D

Dale King

flintridgeparkenfarker vonkerschnauzerheiden III (joe) said:
I admit being confused over this issue. In my mind, if I can change the
value of a variable/object in a calling function via a parameter, then it
seems like I have a reference to a memory location. I can't help wondering
if this isn't an exercise in semantics. Either way, it doesn't matter--I
can't get this thing to work anyway.

It is not just an academic exercise in semantics. There are precisely
defined semantics for what passing parameters mean, but unfortunately people
get confused due to others applying different meaning for the same words.

You cannot change the value of an object. Objects do not have values. You
can change the values of fields within the object.

Most importantly, realize that you cannot in any way, shape, or form pass an
object to a method in Java. This is due to that fact that there are no
variables in Java that actually hold objects. All objects in Java live on
the heap. There are variables that can hold a reference to allow you to
access fields and methods of the object and you can pass the value of one of
these variables, but that is not passing the object.
 
R

Ryan Stewart

Thomas G. Marshall said:
Ryan Stewart <[email protected]> horrified us with:

...[snipitty doo dah]...
More details? Code maybe? And if you're passing strings, you're
passing by reference.

This is a conversation that keeps raising its head up out of the lagoon to
scare the natives.

Java is pass by value /only/.

When you pass an object, you are passing a reference to the object /as a
value/.
Okay, semantic difference I guess. What I meant to convey to him was that
passing a String is no different than passing any other object. I assume
what you're saying is that since a variable can only hold a primitive type
or a reference to an object and there is no way to pass the memory location
of a variable to a method, there is no pass-by-reference in Java, correct?
I'd never considered how differently Java handles its variables.
 
T

Thomas G. Marshall

Ryan Stewart said:
"Thomas G. Marshall"
Ryan Stewart <[email protected]> horrified us with:

...[snipitty doo dah]...
More details? Code maybe? And if you're passing strings, you're
passing by reference.

This is a conversation that keeps raising its head up out of the
lagoon to scare the natives.

Java is pass by value /only/.

When you pass an object, you are passing a reference to the object
/as a value/.
Okay, semantic difference I guess. What I meant to convey to him was
that passing a String is no different than passing any other object.
I assume what you're saying is that since a variable can only hold a
primitive type or a reference to an object and there is no way to
pass the memory location of a variable to a method, there is no
pass-by-reference in Java, correct?

No, you're missing it, but your heart's in the right place.

If you were to pass the memory location of an object, that would be passing
the memory location of the object by value.

Seems like semantics, but it's not: these things have very defined values in
computer science, and they are getting confused, sometimes by even authors.

BUT, what is acceptable common conversational usage (IMHO), however also
wrong, is the notion that you can pass an object in java. You cannot: you
pass the non-arithmetic pointer to it by value. Completely non-acceptable
is saying that Java can be pass-by-reference.

"pass by reference" and "passing a reference by value" are completely
different phenomenon. Quite frankly, I wish both terms would go away,
'cause they are botched often, but they /do/ mean specific things.

C, for example, was pass by value /only/ as well. Period, end of story.

If java were pass by reference, then the passing of a variable would allow a
method to treat that formal parameter as the left-hand-side of an argument,
and alter the original actual parameter. Wazzit mean? Here:

1. If java were pass-by-reference:

int a = 5;
changeIt(a);
//a might be changed at this point

2. Since java is pass-by-value:

int a=5;
changeIt(a);
//a is still 5 at this point no matter /what/ changIt() does.

But, ah ha! This is also true:

3. Java's Pass-by-value:

Object thang;
changeIt(thang);
// thang cannot be altered, but the object it points to can be.

Issue 2 is important, issue 3 is important but conversationally seems ok to
me to say that an "object is passed to a method". But /never/ that it is
"pass-by-reference".
 
D

Dale King

Thomas G. Marshall said:
Ryan Stewart said:
"Thomas G. Marshall"
Ryan Stewart <[email protected]> horrified us with:

...[snipitty doo dah]...

More details? Code maybe? And if you're passing strings, you're
passing by reference.

This is a conversation that keeps raising its head up out of the
lagoon to scare the natives.

Java is pass by value /only/.

When you pass an object, you are passing a reference to the object
/as a value/.
Okay, semantic difference I guess. What I meant to convey to him was
that passing a String is no different than passing any other object.
I assume what you're saying is that since a variable can only hold a
primitive type or a reference to an object and there is no way to
pass the memory location of a variable to a method, there is no
pass-by-reference in Java, correct?

No, you're missing it, but your heart's in the right place.

If you were to pass the memory location of an object, that would be passing
the memory location of the object by value.

Seems like semantics, but it's not: these things have very defined values in
computer science, and they are getting confused, sometimes by even authors.

BUT, what is acceptable common conversational usage (IMHO), however also
wrong, is the notion that you can pass an object in java. You cannot: you
pass the non-arithmetic pointer to it by value. Completely non-acceptable
is saying that Java can be pass-by-reference.

"pass by reference" and "passing a reference by value" are completely
different phenomenon. Quite frankly, I wish both terms would go away,
'cause they are botched often, but they /do/ mean specific things.

C, for example, was pass by value /only/ as well. Period, end of story.


That is true, but I think it is important to note that there is a difference
here. C differs from Java in that you can actually pass a pointer to a
variable in C. That pointer is passed by value, but you can pass something
to function giving that function the ability to modify a variable. In Java
there is absolutely no way to pass something to allow a method to directly
modify a variable (which is a good thing). You can modify the state of
objects, but once again objects are not variables and are not stored in
variables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top