Had a lil Eureka moment today!
Dug into how Java handles method parameters....
It’s pass-by-value, but when it’s objects, it feels like pass-by-reference
Turns out, it's really "pass the reference by value"
@piyushgarg_dev
#Java #javaDSA
ALT A Java program where a Dog object named myDog is passed to a method changeName, only the name property is modified inside the method without reassigning the object. The output is "Max", showing that changes to the object’s internal state do reflect outside the method because the reference still points to the same object.
ALT A Java program where a Dog object named myDog is passed to a method changeName. Inside changeName, a new Dog object is assigned to the parameter and its name is set to "Max". However, the original myDog remains unchanged. The output is "Bruno", showing that Java passes the reference by value, so reassigning inside the method does not affect the original object.