943,486 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 31301
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 8th, 2005
0

need help understandin how to pass by reference.

Expand Post »
can someone explain to me how i can pass the array "test" and the int "total" back to the main by reference.


public static void CalculateAverage() throws IOException
{
int[] test=new int[7];
int count;
int total=0;
for (count = 0;count < 5; count++)
{
test[count] = Integer.parseInt(tokenizer.nextToken());
total = test[count] + total;
}
}
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Mar 8th, 2005
0

Re: need help understandin how to pass by reference.

Quote originally posted by SyLk ...
can someone explain to me how i can pass the array "test" and the int "total" back to the main by reference.


public static void CalculateAverage() throws IOException
{
int[] test=new int[7];
int count;
int total=0;
for (count = 0;count < 5; count++)
{
test[count] = Integer.parseInt(tokenizer.nextToken());
total = test[count] + total;
}
}
the " int total" can only be passed by value, not reference, like all primivitive types of java, however, if you really wanted to bypass this you could declare "static int total" as a globabl variable, but this can tend to be bad practice. The test array can be passed through reference, but you might want to change the scope of it and put it outside of the method maybe in main, and then pass the array to the CalculateAverage() method. First off, a tip for java is that your "main" method should probably only consist of one or two lines of code. Let me give you an example of how i would do something like this, I will keep main simple, pass the array by reference, and the int by value.


class Example {

public static void main (String []args)
{
Example p = new Example ();
p.getStarted;
//You could also do "this.getStarted "

}

public void getStarted()
{
int [] test = new int [7];
int total = CalculateAverage(total, test);
}

public int CalculateAverage(int total, int test []) throws IOException
{



for (x = 0;x < 5; xt++)
{
test[x] = Integer.parseInt(tokenizer.nextToken());
total = test[x] + total;
}
return total;
}



I hope this helps.
Reputation Points: 13
Solved Threads: 0
Newbie Poster
smagee12 is offline Offline
19 posts
since Oct 2004
Mar 8th, 2005
0

Re: need help understandin how to pass by reference.

Java ALWAYS uses pass by value, so your question makes no sense.

Maybe you THINK objects are passed by reference but they aren't. What's passed are references to objects and these are passed by value.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 8th, 2005
0

Re: need help understandin how to pass by reference.

Quote originally posted by jwenting ...
Java ALWAYS uses pass by value, so your question makes no sense.

Maybe you THINK objects are passed by reference but they aren't. What's passed are references to objects and these are passed by value.

Passing a reference of an object thorugh value is passing by reference.
Thats what passing by reference is in java.

Actually, objects are not really passed at all, and the values are the references, yes, but even though there is debate on this topic, the value is a reference, so its being passed by reference.
Reputation Points: 13
Solved Threads: 0
Newbie Poster
smagee12 is offline Offline
19 posts
since Oct 2004
Mar 8th, 2005
0

Re: need help understandin how to pass by reference.

If it's just calculating the average, do you have to return both of those arrays? I mean, you could just calculate the average and return an int value, instead of returning void. Then you could call the method from main.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 8th, 2005
0

Re: need help understandin how to pass by reference.

>Passing a reference of an object thorugh value is passing by reference.
No, the reference is passed by value, so it's not passing by reference. Google for "java pass reference", it's a common misunderstanding.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 8th, 2005
0

Re: need help understandin how to pass by reference.

Quote originally posted by Narue ...
>Passing a reference of an object thorugh value is passing by reference.
No, the reference is passed by value, so it's not passing by reference. Google for "java pass reference", it's a common misunderstanding.

Aye, i understand what you, and the 2nd poster, and the google searches are saying.

I understand its not "pass by reference", instead, the reference is passed by value. I understand trust me. However, do you really know what that means?

"
When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object. "

Do you see how the pass-by-value changes definition here?

This is due, beacuase the reference is the value, and with that reference you can change the object. Thus, you can logically say, it is pass by reference, since the reference = value.

If you want to get sticky to terminology, you are right, you can not pass by reference, only value, but the OP is not trying to understand this.

I think i can safely say, that when you are talking java, you can say objects are passes by reference, since it invokes differently than with primitive types.
Reputation Points: 13
Solved Threads: 0
Newbie Poster
smagee12 is offline Offline
19 posts
since Oct 2004
Mar 8th, 2005
0

Re: need help understandin how to pass by reference.

>However, do you really know what that means?
Yes, yes I do.

>When the argument is of primitive type, pass-by-value means that the method cannot change its value.
Incorrect. When the argument is of non-final primitive type, pass-by-value means that the method can change its value all it wants, but the original variable passed (if it was even a variable) will not reflect the changes.

>Do you see how the pass-by-value changes definition here?
A "reference" in Java is closer to a pointer in other languages than a true reference. The reference itself is basically a pointer, and it points to another object. Because a copy of the reference still points to the same object, you can make changes. This still follows the rules of passing by value, and still fails to follow the rules of passing by reference. It's truly unfortunate that references in Java are named as they are because it creates confusion.

>you can say objects are passes by reference, since it invokes differently than with primitive types.
Doing so is anything but safe because the misunderstanding creates a nasty and common pitfall. That's what happens when you try to make things simpler by sacrificing accuracy.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 8th, 2005
0

Re: need help understandin how to pass by reference.

Quote originally posted by Narue ...
When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object. "

I should have cited this, its from java.sun.com. So i doubt this is incorrect

http://java.sun.com/docs/books/tutor...arguments.html

Besides that, I agree with everything else you say Narue.
Reputation Points: 13
Solved Threads: 0
Newbie Poster
smagee12 is offline Offline
19 posts
since Oct 2004
Mar 8th, 2005
0

Re: need help understandin how to pass by reference.

can you delete posts on these forums?
Reputation Points: 13
Solved Threads: 0
Newbie Poster
smagee12 is offline Offline
19 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help with Java Threads
Next Thread in Java Forum Timeline: Console Window Code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC