Is Java pass-by-reference or pass-by-value?
Is Java pass-by-reference or pass-by-value?
Java is strictly pass-by-value. This means that when a variable is passed to a method, a new copy of the variable is created and used in the method. If the variable holds a primitive data type, the value of the primitive is copied. If the variable holds an object reference, the value of the reference (the address of the object in memory) is copied, not the object itself.
When you pass an object reference to a method, you are passing the reference's value, so the method receives a copy of the reference. Any changes to the object through this reference will affect the original object because both the original reference and the copied reference point to the same object in the heap. However, if you change the reference itself to point to a different object, the original reference outside the method will not be affected.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.