Hi guys,

I found this thread (http://www.daniweb.com/forums/thread19814.html) that gave me guidance on this topic but I wanted to further develop my understanding.

I understand that in Java, everything 'passes by value', even references. That is, a copy of the value is passed to the parameter so the original value is left unchanged.

In the cases of when Java "passes-by-reference" it is still passing by value as its passing the value of a copy of the reference to the object for example: a balloon analogy:

Balloon = object
String connected to the balloon = reference

So when "passing-by-reference", what's happening is that a copy of the reference to the original object is being passed enabling the original value to be modified? In keeping with the balloon analogy, would it be correct to say that the object (the balloon) now has 2 references (strings) attached to it, that is, the original reference and the copied reference being passed?

Thanks.

Dani AI

Generated

Yes — Java is always pass-by-value. What gets copied is the variable’s value. For primitives that value is the primitive itself; for objects the value is the reference. That means a method receives a copy of the reference, so both the caller’s variable and the method parameter point at the same object. ’s balloon analogy is accurate: two strings can be attached to the same balloon, so mutating the balloon via either string is visible to both.

A small Java example that makes the distinction clear:

class Balloon {
    int air;
    Balloon(int a) { air = a; }
}

static void inflate(Balloon b) { b.air += 10; }   // mutates the object
static void replace(Balloon b) { b = new Balloon(0); } // reassigns the parameter only

public static void main(String[] args) {
    Balloon my = new Balloon(5);
    inflate(my);   // my.air becomes 15
    replace(my);   // my still refers to the original Balloon (air is still 15)
}

Notes and common confusions: String is immutable, so methods that seem to “change” a String actually create a new String — the caller’s reference is not changed by parameter reassignment. Arrays are objects and follow the same rule (you can mutate elements, but assigning the parameter to a new array doesn’t change the caller). Marking a parameter final prevents reassigning that parameter variable, but does not prevent mutating the referenced object.

Practical tip: if you need the caller to see a different reference, return the new object and assign it in the caller, or wrap the reference in a mutable holder. The links shared by and the confirmation from line up with this explanation.

Recommended Answers

All 3 Replies

Thanks for the links, makes sense now.

... would it be correct to say that the object (the balloon) now has 2 references (strings) attached to it, that is, the original reference and the copied reference being passed?

Yes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.