I am writing a method that processes a string (to extract something):
public static String myProcessor (String myInput) {...
I want to have some String object inside this method, that I can apply my transformations on (workString = workString.replace(...
)
However, I don't want to change the original string (via the reference given to my method, i.e.:
myProcessor(originalString)
) - because there may be method calls after my method has been called, which also want to process/extract something from the original string, before my method call
I am still learning Java and, according to my book (Deitel & Deitel - "Java How To Program"):
~"primitive types are pass-by-value. Other variable types are for objects and those are pass-by-reference and therefore variable assignment of an object is passing the reference to the same object. A change in the object referenced by one variable can be seen when reading the other variable (if Object var2 = var1
)"
(This isn't verbatim, just what I understood from it!)
So, I thought I'd try: String workString = new String(myInput);
and this should create a new String object, using data from myInput
, but I could manipulate workString
anyway I want, without worrying I am making changes to an object which is used elsewhere.
IntelliJ suggested that this is redundant, and I can just do: String workString = myInput
, which got me confused...
Is IntelliJ right here?
Actually, do I even need to create a local-scope workString
, or references are "detached" once a variable "arrives" into a method? (i.e.: inside myProcessor
, myInput
is no longer holding a reference to the originalString
from the call for myProcessor
, but it creates a new String object?
Could you also please mention briefly if your answer is regarding just String objects or all objects?
I have a feeling that perhaps String is somewhere between a Primitive type and an Object type and has special rules applied to it...