Java Objects


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Java Objects

Hybrid View

  1. #1
    Raj Guest

    Java Objects


    Hi

    I am very new to Java. I have heard that objects in Java are passed by reference
    and not by value.

    1. Is this correct
    2. What is the main difference between passing by reference and passing by
    value. What efficiciency does this gain you. If possible, please give an
    example to clarify.



  2. #2
    Manoj Guest

    Re: Java Objects


    "Raj" <jstudent01@hotmail.com> wrote:
    >
    >Hi
    >
    >I am very new to Java. I have heard that objects in Java are passed by reference
    >and not by value.
    >
    >1. Is this correct
    >2. What is the main difference between passing by reference and passing

    by
    >value. What efficiciency does this gain you. If possible, please give an
    >example to clarify.
    >
    >



  3. #3
    Tom Duffy Guest

    Re: Java Objects


    Hello Raj:

    1. Yes, Java objects are passed by reference.

    2. Simply put, when you pass an object to a method, you are directly manipulating
    the object. Should your method change the object, the changes are permanent.

    When you pass a primitive data type, you are passing a copy of the value
    of the data. Should your method change the value of the copy of the data,
    the original remains unchanged outside the method.

    Tom Duffy

    "Raj" <jstudent01@hotmail.com> wrote:
    >
    >Hi
    >
    >I am very new to Java. I have heard that objects in Java are passed by reference
    >and not by value.
    >
    >1. Is this correct
    >2. What is the main difference between passing by reference and passing

    by
    >value. What efficiciency does this gain you. If possible, please give an
    >example to clarify.
    >
    >



  4. #4
    Brent Worden Guest

    Re: Java Objects


    Tom Duffy <td4729@hotmail.com> wrote in message
    news:38eb54b7$1@news.devx.com...
    > 1. Yes, Java objects are passed by reference.


    This isn't exactly correct. Objects are not passed as parameters at all.
    References to objects are passed and they are passed by value. In fact,
    every parameter is passed by value in Java.

    Since your passing copies of references, its like passing by reference
    except for one subtle condition: assignments of reference parameters inside
    methods are not realized by the caller.

    For example:

    void changeValue(String reference)
    {
    reference = "new string";
    }

    void caller()
    {
    String str = "old string";
    changeValue(str);
    // ASSERT(str.equals("new string"));
    }

    In Java, the assertion is false. In a true pass-by-reference scenario, the
    assertion would be true.

    Brent Worden
    http://www.Brent.Worden.org



  5. #5
    Tom Duffy Guest

    Re: Java Objects


    Hello Brent:

    You are absolutely correct and thanks for correcting my post.

    Tom Duffy

    "Brent Worden" <brent.worden@javelinsolutions.com> wrote:
    >
    >Tom Duffy <td4729@hotmail.com> wrote in message
    >news:38eb54b7$1@news.devx.com...
    >> 1. Yes, Java objects are passed by reference.

    >
    >This isn't exactly correct. Objects are not passed as parameters at all.
    >References to objects are passed and they are passed by value. In fact,
    >every parameter is passed by value in Java.
    >
    >Since your passing copies of references, its like passing by reference
    >except for one subtle condition: assignments of reference parameters inside
    >methods are not realized by the caller.
    >
    >For example:
    >
    >void changeValue(String reference)
    >{
    > reference = "new string";
    >}
    >
    >void caller()
    >{
    > String str = "old string";
    > changeValue(str);
    > // ASSERT(str.equals("new string"));
    >}
    >
    >In Java, the assertion is false. In a true pass-by-reference scenario,

    the
    >assertion would be true.
    >
    >Brent Worden
    >http://www.Brent.Worden.org
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center