Jump to content

Java Method Object reference


TheWind

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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