Coffeehouse Thread

49 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

object pass by value/reference

Back to Forum: Coffeehouse
  • User profile image
    anand.t

    Hi guys,
     I just got a perliminary call for an interview and one of the questions was how do you pass object parameters in java. It is pass by value right?? Perplexed

    And a related question just to confirm in  C# it is pass by reference right?

    EDIT: I just confirmed it is pass by value in java. Cool..now I hope i clear this first leve of interview Cool

  • User profile image
    Royal​Schrubber

    anand.t wrote:
    Hi guys,
     I just got a perliminary call for an interview and one of the questions was how do you pass object parameters in java. It is pass by value right??

    And a related question just to confirm in  C# it is pass by reference right?


    In java objects are always passed by reference, because in their essence they are only pointers to memory locations where real object are. For the same reason you also cannot have objects in stack frame only references. On contrast basic atomic types (like char, int, double) are passed by value and can lie on stack.

  • User profile image
    anand.t

    RoyalSchrubber wrote:
    

    In java objects are always passed by reference, because in their essence they are only pointers to memory locations where real object are. For the same reason you also cannot have objects in stack frame only references. On contrast basic atomic types are passed by value and can lie on stack.


    hey royal even objects are passed by value.

    "Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level. "

    http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html

  • User profile image
    littleguru

    Structs (Types that inherit from System.ValueType) are passed to a method by value. Classes (Types that inherit from System.Object) are passed in by reference.

    Structs are managed on the stack. Classes are managed on the heap. The stack is self managed (does the clean up when leaving a method) and the heap is cleaned up by the garbage collector (the wonderful GC in .NET).

    Are classes in JAVA passed in by value? That doesn't make any sense to me...

  • User profile image
    littleguru

    anand.t wrote:
    
    RoyalSchrubber wrote:
    

    In java objects are always passed by reference, because in their essence they are only pointers to memory locations where real object are. For the same reason you also cannot have objects in stack frame only references. On contrast basic atomic types are passed by value and can lie on stack.


    hey royal even objects are passed by value.

    "Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level. "

    http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html


    That's passed in by reference. Classes are managed on the heap.

  • User profile image
    Royal​Schrubber

    littleguru wrote:
    Are classes in JAVA passed in by value? That doesn't make any sense to me...


    You meant instances of classes - objects, right? As far as I know objects are internally represented as pointers (to real locations). You can't otherwise explain for example this:

    class foo{
       int something;
       foo next;
    }

    We can interpret this as a basic list structure. If 'next' slot is not a pointer (reference) then this structure cannot exist. Ergo, objects are pointers. Also books explain class types as 'reference types'.

  • User profile image
    ScanIAm

    anand.t wrote:
    
    RoyalSchrubber wrote:
    

    In java objects are always passed by reference, because in their essence they are only pointers to memory locations where real object are. For the same reason you also cannot have objects in stack frame only references. On contrast basic atomic types are passed by value and can lie on stack.


    hey royal even objects are passed by value.

    "Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level. "

    http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html


    I'm pretty sure that the above description is what "passed by reference" means.

  • User profile image
    anand.t

    littleguru wrote:
    
    That's passed in by reference. Classes are managed on the heap.


    check here

    http://javadude.com/articles/passbyvalue.htm

    "Objects are not passed by reference. A correct statement would be Object references are passed by value"

    there is a difference. At the time of the interview I did not remember this, but just said it. Thankfully they did not dig further into java.

    Seriouly I hate this. THe job was for .net developer, but they are asking java questions Expressionless

  • User profile image
    littleguru

    anand.t wrote:
    
    littleguru wrote:
    
    That's passed in by reference. Classes are managed on the heap.


    check here

    http://javadude.com/articles/passbyvalue.htm

    "Objects are not passed by reference. A correct statement would be Object references are passed by value"

    there is a difference. At the time of the interview I did not remember this, but just said it. Thankfully they did not dig further into java.

    Seriouly I hate this. THe job was for .net developer, but they are asking java questions


    Oh you mean the difference between "int" and "Integer" in java? Well int is a value type and passed in by value ... Integer is a reference type (class) and passed in by reference. You can't pass in a instance of int into a class that expects Object. You need to box that manually in JAVA - it needs to be wrapped into an instance of Integer.

  • User profile image
    Royal​Schrubber

    ScanIAm wrote:
    
    anand.t wrote:
    
    RoyalSchrubber wrote:
    

    In java objects are always passed by reference, because in their essence they are only pointers to memory locations where real object are. For the same reason you also cannot have objects in stack frame only references. On contrast basic atomic types are passed by value and can lie on stack.


    hey royal even objects are passed by value.

    "Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level. "

    http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html


    I'm pretty sure that the above description is what "passed by reference" means.


    ++, at the end, everything is passed by value, becuase the values in stack frame need to be changed, if they are real objects or any order of pointer.

    I think this talk would be much more relevant when you talk about c++, where you can actually choose how you want to pass variables.

  • User profile image
    littleguru

    RoyalSchrubber wrote:
    
    littleguru wrote:
    Are classes in JAVA passed in by value? That doesn't make any sense to me...


    You meant instances of classes - objects, right? As far as I know objects are internally represented as pointers (to real locations). You can't otherwise explain for example this:

    class foo{
       int something;
       foo next;
    }

    We can interpret this as a basic list structure. If 'next' slot is not a pointer (reference) then this structure cannot exist. Ergo, objects are pointers. Also books explain class types as 'reference types'.


    Yes... Classes (instances of classes) are managed on the heap. You then only work with a pointer to the address on the heap... In C++ you had to work with the pointers on your own. and delete the class instance from the heap when you didn't need it anymore.

    In JAVA or .NET that is all done by the garbage collector. It detects when an instance on the heap is no longer reachable (holds a freachable list) and deletes it afterwards.

    Reference types (in .NET inheriting from System.Object) are always put on the heap and you get only a pointer (a reference to that object) to work with. You can pass that pointer to a method (passing an argument by reference). The method can manipulate the class instance by using the pointer that has been provided. If you return from the method the external method sees that changes, because the object on the heap has been manipulated.

    Value Types (structs - in .NET inheriting from System.ValueType) are most likely put on the stack. They are passed in by value (the instance is copied!). It's not a pointer that is passed in by default, but the value itself. If you change the instance in the method that got the struct passed in, the changes happen on the copied instance - the one that is passed in by value. The outer method doesn't see the changes therefore.

  • User profile image
    Royal​Schrubber

    I think the definition of passed by reference would be:

    void foo(X param){
       X temp;
       temp = (new instance of X)
       param = X;
    }

    If we call a function foo and give it some variable, if that variable is changed after foo returns then variable was sent by reference. If the value remains intact then variable was sent by value.

    In java if X is atomic type then variable will be sent by value if it is object then it is sent by reference.

    Example of calling function would be:

    void main(){
       X someVariable;
       foo(someVariable);
    }

  • User profile image
    littleguru

    RoyalSchrubber wrote:
    I think the definition of passed by reference would be if:

    void foo(X param){
       X temp;
       temp = (new instance of X)
       param = X;
    }

    If we call a function foo and give it some variable, if that variable is changed after foo returns then variable was sent by reference. If the value remains intact then variable was sent by value.

    In java if X is atomic type then variable will be sent by value if it is object then it is sent by reference.

    Example of calling function would be:

    void main(){
       X someVariable;
       foo(someVariable);
    }


    What are you talking about???

    Passed as reference in C++:

    Foo *foo = new Foo();
    void Bar(Foo* foo)
    {
        foo->Bla();
    }

    Passed as value in C++:

    Foo foo();
    void Bar(Foo foo)
    {
        foo.Bla();
    }

    The first sample passes in a pointer and creates the Foo instance on the heap. The second sample passes in the Foo instance as a copy! It is also created on the stack and not on the heap.

    You could also pass in the object on the heap as pointer in the second example. Which is not good (if you would store the instance in another class or so), because if the outer method (where foo has been created) is left the instance is deleted! The stack is self-maintaining and all instances that are created within a stack frame (= method) are deleted if the method is left!

  • User profile image
    littleguru
  • User profile image
    anand.t

    littleguru wrote:


    thanks I was just looking for something to compare with the java article I posted above.

  • User profile image
    Royal​Schrubber

    littleguru wrote:
    
    What are you talking about???


    First let me explain some historical facts. C did not have ability to pass variable in manner that is technically considered 'by reference'. If you wanted to pass a variable without physically copying it you would do it with pointers. When you pass in a pointer it's always passed by value. Technically in both examples you passed variables as values.

    C++ came up with new notation which goes something like this:

    bar(int& param){
       param = 3;
    }

    It's construct that wraps pointers so that you don't have to deal with them. Internally pointer is still passed (by value), but result would be that if you change param it would also affect variable that was passed into bar function.

  • User profile image
    littleguru

    RoyalSchrubber wrote:
    First let me explain some historical facts. C did not have ability to pass variable in manner that is technically considered 'by reference'. If you wanted to pass a variable without physically copying it you would do it with pointers. When you pass in a pointer it's always passed by value. Techniacally in both examples you passed varibles as values.


    OMG! You refer to passing in an Int32 as pointer... and that this pointer is passed in by value... Expressionless

    Well - in C#, .NET, JAVA passing in by reference is passing in a pointer! If the instance is managed on the heap it is always passed in by reference. If the instance is managed on the heap it is passed in by value.

    The difference is if a pointer or a copy of the instance (struct) itself is passed in...

  • User profile image
    wkempf

    This one annoys me, and so many people here, including those who should know better, are getting it wrong.

    In both Java and .NET langauges, the default is pass by value.  In Java, I don't believe it's possible to pass by reference.  In .NET, you can do this by specifying it (ref and out keywords in C#).

    How you pass parameters has NOTHING to do with whether or not the object lives on the heap or the stack.  There is a difference between "reference types" and "pass by referece".  Here's some code to illustrate that .NET and Java are pass by ref (you'll have to change the syntax to match your language, but the code itself illustrates things nicely):

    string foo = "bar";
    Xyzzy(foo);
    Console.WriteLine(foo); // "bar" if pass by value, "baz" if pass by ref

    void Xyzzy(string foo)
    {
       foo = "baz";
       Console.WriteLine(foo); // "baz"
    }

    If you've got experience with other languages, it's common to misunderstand this simple concept, but you should learn this, because you have a flawed concept of the meaning here even in those other languages. 

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.