Apex inherits many language features from Java, and Java passes everything by value (the object references are also passed by copying their values).
String
is an immutable class, so mutating instances will always create additional copies. Unfortunately, Apex does not offer a StringBuilder
class that would help with some pain.
If it is just passing around that you are concerned with (and not modifying it), then user320's suggestion will work fine. Wrapping a String
inside an object (or in a array) will allow you to pass the reference to that object by value. That will save a lot of memory if you need just read-only access because only the wrapping operation might copy the String
, and each subsequent passing will not copy the String
.