CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2005
    Posts
    25

    Post java is pass by refference or value

    Hi,

    Actualy java is pass by reference or pass by reference.I Know it is pass by value,But HOW?and what are the logic behind all these.and why this qs all put? pls I want the the detail theroy with examples.

    Thanks
    manoj

  2. #2
    Join Date
    Jul 2005
    Location
    Ontario, Canada
    Posts
    107

    Re: java is pass by refference or value

    All objects are passed by reference.
    All primitive data types are passed by value.

    This is a weakness in java that is not in C++. For example, assuming there is a class called Asdf

    Code:
    int main()
    {
         Asdf a = new Asdf();
         function1(a);
    }
    
    void function1(Asdf b)
    {
         b = new Asdf();
    }
    What is happening here is that you are creating a new Asdf called a. Then your passing in a reference to this new Asdf, but it now has a new name "b". "b", in the function is set to point at a new Asdf. However, a is still pointing at the original Asdf. Therefore, this function does nothing.

  3. #3
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: java is pass by refference or value

    It can get a little confusing, some of the explanations out there are unfortunately not very clear about what they're referring to. The links below should help you out a bit.

    http://www-128.ibm.com/developerwork...raxis/pr1.html
    http://www-128.ibm.com/developerwork...val/index.html
    http://javadude.com/articles/passbyvalue.htm

    I wouldn't necessarily call it a weakness, more like a compromise, giving up a little bit of flexibility for some security.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  4. #4
    Join Date
    Jul 2005
    Location
    Ontario, Canada
    Posts
    107

    Re: java is pass by refference or value

    I suppose it is a compromise. Pointer arithmetic is something I'm definetly not going to miss.

  5. #5
    Join Date
    Jul 2005
    Posts
    1

    Red face Re: java is pass by refference or value

    The language is inconsistent and counterintuitive, but one thing's for sure: Java is pass by value, in every case. If you answer otherwise on Sun's Programmer Cert. exam, you will be marked wrong.

    A value is something stored in a variable (or more precisely, something stored in the variable's location on the stack).

    The only types of values in Java are primitives and references. A primitive is a value with a literal representation. A reference is a value that points/refers to an object.

    Whenever you assign a value to a variable or pass a value as an argument, you are actually copying the value into the target variables location in the stack. This is known as "assignment/passing by value".

    An object is not a value. It lives on the heap, not in a variable's location on the stack. Furthermore, an object is never assigned or passed, only its reference is, and as discussed above, the reference is copied (passed by value).

    Java in a Nutshell explains this in greater detail, if you want more insight. I hope this was helpful.

  6. #6
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: java is pass by refference or value

    Whatever you name it, it's the same thing.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  7. #7
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: java is pass by refference or value

    Quote Originally Posted by Chris256
    All objects are passed by reference.
    mmmh, kind of.. a new pointer is attached to the object and made available to the called method. what actually gets passed is a value of a pointer that is a reference to the object.

    This is a weakness in java
    you ought to have written "In my antiquated opinion, this is a weakness in java"

    of course, my opinion that your opinion is antiquated, is just my opinion..

    Code:
    int main()
    {
         Asdf a = new Asdf();
         function1(a);
    }
    
    void function1(Asdf b)
    {
         b = new Asdf();
    }
    yes, but we dont do things like that any more. If you loaned your mp3 player to a friend to put some tunes on, would you expect him to give you back a whole new ipod, with the tunes on? what if your mum bought you your ipod and you were really sentimentally attached to it? you'd be upset..
    what if what he gave you back wasnt even an ipod but some low spec $30 mp3 player?

    this was how we did things one upon a time, but now we have more secure ways of programming.. we dont give object A to object B for object B to fiddle with as it likes.. now if there is any fiddling to be done, its done through accessor methods on object A. if object B does something to A that A cannot do, support or otherwise doesnt like, then the operation stops, not the entire program.

    What is happening here is that you are creating a new Asdf called a. Then your passing in a reference to this new Asdf, but it now has a new name "b". "b", in the function is set to point at a new Asdf. However, a is still pointing at the original Asdf. Therefore, this function does nothing.
    what's happening here is actually this:

    //make objectA
    (pointerA) ----> [objectA]
    //function1(a); //actually in java we have METHODS, not functions
    (pointerA) ----> [objectA] <---- (pointerB)
    //b = new asdf();
    (pointerA) ----> [objectA] &#160;&#160;&#160;&#160;&#160;&#160;&#160; (pointerB) ----> [objectB]


    so the function (method) does actually do something; it makes a new object and point B to it instead. unfortunately then, it goes out of scope and both pointer B and object B are dissolved.

    how is this a weakness?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: java is pass by refference or value

    Quote Originally Posted by jcovarru
    The language is inconsistent and counterintuitive.
    care to give an example?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

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

Featured