Results 1 to 12 of 12

Thread: Pass ByRef in Java

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  3. #3
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Pass ByRef in Java

    Whatever ...
    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.

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Pass ByRef in Java

    Quote Originally Posted by CornedBee
    Whatever ...
    And you can prove otherwise?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  7. #7
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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?

  8. #8

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    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

  9. #9
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Pass ByRef in Java

    Return the object. As for returning multiple objects, I've never run into that situation.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  11. #11

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    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

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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
  •  



Click Here to Expand Forum to Full Width