need help understandin how to pass by reference.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

need help understandin how to pass by reference.

 
0
  #1
Mar 8th, 2005
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;
}
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 19
Reputation: smagee12 is an unknown quantity at this point 
Solved Threads: 0
smagee12 smagee12 is offline Offline
Newbie Poster

Re: need help understandin how to pass by reference.

 
0
  #2
Mar 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: need help understandin how to pass by reference.

 
0
  #3
Mar 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 19
Reputation: smagee12 is an unknown quantity at this point 
Solved Threads: 0
smagee12 smagee12 is offline Offline
Newbie Poster

Re: need help understandin how to pass by reference.

 
0
  #4
Mar 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: need help understandin how to pass by reference.

 
0
  #5
Mar 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: need help understandin how to pass by reference.

 
0
  #6
Mar 8th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 19
Reputation: smagee12 is an unknown quantity at this point 
Solved Threads: 0
smagee12 smagee12 is offline Offline
Newbie Poster

Re: need help understandin how to pass by reference.

 
0
  #7
Mar 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: need help understandin how to pass by reference.

 
0
  #8
Mar 8th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 19
Reputation: smagee12 is an unknown quantity at this point 
Solved Threads: 0
smagee12 smagee12 is offline Offline
Newbie Poster

Re: need help understandin how to pass by reference.

 
0
  #9
Mar 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 19
Reputation: smagee12 is an unknown quantity at this point 
Solved Threads: 0
smagee12 smagee12 is offline Offline
Newbie Poster

Re: need help understandin how to pass by reference.

 
0
  #10
Mar 8th, 2005
can you delete posts on these forums?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC