| | |
need help understandin how to pass by reference.
![]() |
•
•
Join Date: Oct 2004
Posts: 26
Reputation:
Solved Threads: 0
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;
}
}
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;
}
}
•
•
Join Date: Oct 2004
Posts: 19
Reputation:
Solved Threads: 0
•
•
•
•
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;
}
}
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.
•
•
Join Date: Oct 2004
Posts: 19
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Oct 2004
Posts: 19
Reputation:
Solved Threads: 0
•
•
•
•
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.
>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.
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.
•
•
Join Date: Oct 2004
Posts: 19
Reputation:
Solved Threads: 0
•
•
•
•
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: Problem with String replaceAll method
- Next Thread: Console Window Code
| Thread Tools | Search this Thread |
2dgraphics 3d @param affinetransform android api applet application arc arguments array arrays automation banking binary bluetooth byte chat chatprogramusingobjects class client code color compare component count database design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong problem producer program programming project projectideas read recursion reference replaysolutions rim scanner server set size sms sort sql string swing terminal threads transforms tree ui unicode validation web windows






