|
-
Sep 19th, 2005, 11:42 PM
#1
Thread Starter
PowerPoster
Pass ByRef in Java
Okay so far Java is much the same as C syntax and many other respects, not all... Here is one place that is definatly different -- no dynamic memory access em I to imagine? I found myself stuck when trying to play around and learn some stuff on my own. I needed to pass a string by ref so I could alter the value of the passed String object parameter in a function and the original variable would also change. If definetly tried reference & and dereference *.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Sep 20th, 2005, 08:06 AM
#2
Re: Pass ByRef in Java
Everything is always passed by reference. However, the String class represents an immutable string - once it has been created, it can't be altered. You can pass a StringBuffer or StringBuilder to the method. Or a StringRef object:
Code:
public class StringRef { public String ref; }
Just change the ref object.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 20th, 2005, 03:48 PM
#3
Frenzied Member
Re: Pass ByRef in Java
All object variables are references and java does manipulate objects via reference, but arguments are NOT passed by reference, but by value.
-
Sep 20th, 2005, 03:56 PM
#4
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 20th, 2005, 04:11 PM
#5
Frenzied Member
Re: Pass ByRef in Java
 Originally Posted by CornedBee
Whatever ...
And you can prove otherwise?
-
Sep 20th, 2005, 04:32 PM
#6
Re: Pass ByRef in Java
No, I know you're right. It just doesn't make much difference whether you consider the objects references passed by value, or the objects passed by reference, since you have no alternative in Java anyway. You cannot pass objects by value, and you can't pass references by reference, and the other two options are semantically equivalent.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 20th, 2005, 04:35 PM
#7
Frenzied Member
Re: Pass ByRef in Java
Oh, sorry, thought you were mad or disagreeing
Anways, it doesn't make much sense to me. If you can pass arrays "by reference", then why not other argument types?
-
Sep 20th, 2005, 04:53 PM
#8
Thread Starter
PowerPoster
Re: Pass ByRef in Java
Okay so I am to assume I cannot write a function to manipulate a passed variable?
Like
public static void Add3To(int iNumToAddThreeTo) {
iNumToAddThreeTo = iNumToAddThreeTo + 3;
}
The original variable passed in won't change?
Well that sucks!
Java keeps creeping along as a very useless language to me. C++ is easily more versitile and structured properly.
In my books:
ByVal == Passes in the value to a new variable parameter
ByRef == Passes in the address to the variable parameter to allow modification of the same place in memory via the parameter.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Sep 20th, 2005, 04:56 PM
#9
Frenzied Member
Re: Pass ByRef in Java
Return the object. As for returning multiple objects, I've never run into that situation.
-
Sep 20th, 2005, 05:27 PM
#10
Re: Pass ByRef in Java
I have, once. I handle that by writing a tiny container class.
You can manipulate objects passed to functions, as you are passed a reference to them. But that requires that the objects can be manipulated, which String can't, and neither can the primitive wrappers. You can't change the references themselves. The primitives themselves aren't even objects, and you don't get a reference to them either.
It sounds like a great weakness of the language, but as a matter of fact, the situations where you need it are surprisingly rare.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 20th, 2005, 11:41 PM
#11
Thread Starter
PowerPoster
Re: Pass ByRef in Java
So if I wrote this example using int instead String object, then it is always passed via reference?
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Sep 21st, 2005, 02:30 AM
#12
Re: Pass ByRef in Java
No, int is a primitive and is passed by value.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|