TheWind Posted March 27, 2017 Share Posted March 27, 2017 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. Am I missing something here? Quote Link to comment Share on other sites More sharing options...
Team Cape Posted March 27, 2017 Share Posted March 27, 2017 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 Quote Link to comment Share on other sites More sharing options...
Alek Posted March 27, 2017 Share Posted March 27, 2017 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. 1 Quote Link to comment Share on other sites More sharing options...
TheWind Posted March 27, 2017 Author Share Posted March 27, 2017 Yes, thank you both. I believe my confusion was based on me overlooking immutable classes. Quote Link to comment Share on other sites More sharing options...
Reveance Posted March 27, 2017 Share Posted March 27, 2017 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 Quote Link to comment Share on other sites More sharing options...