1

I am trying to understand how exactly method parameters are passed in Java.

I was able to figure out that java only uses pass by value. I understand how this works for primitive types (like int) but I am a little confused about reference types.

I think that in Java we don't pass objects but object references. But in this case I don't really understand what's going on in this code example:

public class test {

    static String a = "String a";
    static String b = "String b";
    
    public static void main(String[] args) {
            System.out.println(a);     //expected String a
        doSomething(a);
        System.out.println(a);     //expected String b
    }
    
    static void doSomething(String someString) {
        someString = b;
    }
}

I know that this is anything but good practice but I am only using this example to understand what's going on.

So, basically be calling the method doSomething, I am passing a reference to a String object (the reference is stored in the variable called a). But then I try to change this reference inside the method to reference another String object. However, this does not work.

I somehow just assumed that parameters in Java work the same way as io-parameters in pascal procedures (there are no return types, instead you just reassign a pass by reference parameter). That doesn't seem to be right.

The only way I can explain this to myself is, that when calling a method, java does not pass the actual object reference but instead passes a 'copy' of the reference wich points to the same object (same object but different pointers). Can anybody confirm if am right with this or tell please me, what actually happens here?

I would really appreciate your help.

Jan
  • 63
  • 4
  • 1
    Strings are immutable, so there is no pass by reference. – Slevin Aug 23 '23 at 04:55
  • 1
    You are right. It's a copy of the reference, except for Strings. Hence it behaves differently for primitive and Object types. Object references still point to the same object on Heap. If you reassign the non-final reference in the invoked method, the original object's reference is lost. – Sid Aug 23 '23 at 04:56
  • 3
    @Sid "_except for Strings_" – `String` is a reference type, and thus the reference to the object is copied (i.e., the reference is the _value_ being passed). – Slaw Aug 23 '23 at 05:15
  • 2
    @Slevin Mutability of the object is orthogonal to _pass-by-value_ vs _pass-by-reference_ semantics. – Slaw Aug 23 '23 at 05:17
  • 6
    @Jan To summarize the duplicate: Java is **always** _pass-by-value_. This is straightforward for primitive types; their values are just copied. But for reference types, the value being passed is the _reference_ itself. So yes, when you pass an object to a method, the reference to that object is copied, so that you now have two references to the same object in memory. _Reassigning_ one of the references will not reassign the other. However, so long as both reference the same object, modifications made to that object can be seen via both the references. – Slaw Aug 23 '23 at 05:20
  • @Slaw Thanks for your answer. I think I do understand it now. May I ask you, how you know about things like that? I often have questions about such details and why things are done the way they are done. Sometimes it's hard to finde answers to these questions. Is there any source that you could suggest? Maybe the language specification? – Jan Aug 23 '23 at 06:05
  • In code like `String b = "String b"`, think conceptually of the reference variable `b` as *not* holding a `String` object, but as holding a memory address, like an integer number, where the `String` object lives. Then saying "a reference is passed by value" might make more sense. After the line `someString = b` executes, both `someString` and `b` now hold the very same memory address, the same integer number, and both vars can be used to access the same single object living at that address. The catch is that in Java, unlike in C, we programmers never see that memory address directly. – Basil Bourque Aug 23 '23 at 06:20
  • This specific information, I learned mostly from the linked duplicate. Though I will say it took me a while to truly understand what was being said, as I was a beginner at the time. What helped with understanding was mostly experience. Reading books/articles. Understanding, at least at a basic level, how computers work (e.g., how objects are stored in memory, how addresses work, how stacks work, etc.). Recognizing that a lot of these concepts are, well... _conceptual_ (i.e., not language specific). University helped a lot, as courses will teach you things you didn't know you needed to know. – Slaw Aug 24 '23 at 02:40

0 Answers0