Thursday, March 21, 2024

Is Java "pass-by-reference" or "pass-by-value"?

Java is technically considered pass-by-value.

Here's why it can be confusing:

  • In Java, primitive data types (like int or double) are directly copied and passed by value to methods.
  • For objects (which are references), a copy of the reference is passed, not the object itself. This reference points to the object's location in memory.

So, while you might think modifying an object passed to a method would change the original object (pass-by-reference behavior), it actually only modifies the copy of the reference within the method. The original object remains unchanged.

No comments:

Post a Comment

Java + HSM: Secure Key Management in Practice

 Java + HSM: Secure Key Management in Practice   When dealing with sensitive data, cryptographic keys should never leave secure hardware . T...