Jump to content

Java Method Object reference


Recommended Posts

Posted

http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html

http://jonskeet.uk/java/passing.html

 

After reading through these two articles I believe I have corrected my misunderstanding of passing object references to methods. Objects are passed to methods by value only. What I'm confused about is why this picture shows the reference from the method pointing to the same object. From what both articles said the reference and object are copies of the originals, not just the reference.

03-qa-0512-pass1-100158781-orig.gif

Am I missing something here?

Posted

the value of the object is passed into the method, but the method still needs a way to refer to the object. the reference to the object that the method uses is different from the reference to the object that is given.

 

psvm(String[] args) {

      Foo f = new Foo(); //f is a reference to the value given by new Foo()

      doSomething(f); // java will take the value given by the reference f (namely the value of that new Foo) and pass it to doSomehing()

}

 

static void doSomething(Foo f) { 

    f.doAThing(); //in order for the programmer to do something with that value inside of the method though, the programmer needs a way to reference it inside of the method. Thus, the method is using a different reference to the same value

}

This was roughly how it was explained to me in my AP CS course. if i missed anything, someone correct me

Posted

Java uses pass-by-value whereas in a lower-level language like C++ you can pass-by-reference or value. So if you had created an object you could pass around a pointer to that object which is only 4 bytes (32-bit), whereas in Java you would have to pass the object itself which could be much larger (afaik Java would do a deep copy). This is where some of the benefits of using a lower level language come in.

C++:
Object* p = new Object(); //p is only 4 bytes, the actual object is stored somewhere in memory
function(Object* obj) //Accepts a pointer of Obj
 

Maybe this helps you make sense of pointers a bit.

  • Like 1
Posted
4 hours ago, Alek said:

Java uses pass-by-value whereas in a lower-level language like C++ you can pass-by-reference or value. So if you had created an object you could pass around a pointer to that object which is only 4 bytes (32-bit), whereas in Java you would have to pass the object itself which could be much larger (afaik Java would do a deep copy). This is where some of the benefits of using a lower level language come in.

C++:
Object* p = new Object(); //p is only 4 bytes, the actual object is stored somewhere in memory
function(Object* obj) //Accepts a pointer of Obj
 

Maybe this helps you make sense of pointers a bit.

I believe Java doesn't actually pass the object itself, but since it is pass-by-value, the object references are passed by value. However all primitives are just plain passed by value

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...