0
import java.util.HashSet;
import java.util.Set;
public class Main {
    public static void main(String[] args) {
        String input = "original input";
        Set<String> test = new HashSet<>();
        test.add(input);
        input = "modified input";
        test.stream().forEach(name -> System.out.println(name));
        System.out.println(input);
    }
}

Output:

original input
modified input

Is there any structure in Java that I can pass this string object to, as a reference and both outputs would be the same modified input when reading the string object from through the structure and reading the object directly?

Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
sasieightynine
  • 434
  • 1
  • 5
  • 16

4 Answers4

3

Java doesn't have variable references, no. What you can do is wrap it in a mutable object:

class Wrapper {
    private String value;
    Wrapper(String v) {
        this.value = v;
    }
    String getValue() {
        return this.value;
    }
    void setValue(String v) {
        this.value = v;
    }
}

Then:

public static void main(String[] args) {
    String input = new Wrapper("original input");

    Set<Wrapper> test = new HashSet<>();

    test.add(input);

    input.setValue("modified input");

    test.stream().forEach(name -> System.out.println(name.getValue()));

    System.out.println(input.getValue());
}

The value in the set (the reference to the Wrapper object) doesn't change, but you change the state of the object it refers to.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

Not with a String, because String is immutable. You could modify your approach to use a StringBuilder. Like,

StringBuilder input = new StringBuilder("original input");
Set<StringBuilder> test = new HashSet<>();
test.add(input);
input.setLength(0);
input.append("modified input");
test.stream().forEach(name -> System.out.println(name));
System.out.println(input);

Outputs (as requested)

modified input
modified input
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

No. Java does not have a special structure to pass a String as a reference, because String is immutable and cannot be modified (every time whey you modify a String, a new String is being created).

To modify a String in a new method, you have to get the original string, build a new and modified String instance and retrieve it:

public static void main(String... args) {
    String str = "original string"; // str = original string
    str = updateString(str);        // str = original string_smth.new

}

private static String updateString(String str) {
    return str + "_smth.new";
}

As an alternative, you could build a custom wrapper and use it:

public static void main(String... args) {
    StringWrapper str = new StringWrapper("original string");   // str = original string
    updateString(str);              r = original string_smth.new

}

private static void updateString(StringWrapper str) {
    str.set(str.get() + "_smth.new");
}

private static final StringWrapper {
    private String str;

    public StringWrapper(String str) {
        this.str = str;
    }

    public String get() {
        return str;
    }

    public void set(String str) {
        this.str = str;
    }

    public String toString() {
        return str;
    }
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

When you do something like this:

String input = "original input";

that means you're creating a new string in the string pool: https://www.java67.com/2014/08/difference-between-string-literal-and-new-String-object-Java.html

Then, when you're trying to reassign it like this:

input = "modified input";

that only means the you create one more string in the string pool and making 'input' to be a reference to the new string.

So, adding old string to collection - you're adding the string's memory address value to that collection object: https://www.javaworld.com/article/2077424/learn-java-does-java-pass-by-reference-or-pass-by-value.html As the result it's not aware of the future manipulations with the input.

To conclude: to do, what you need, you will have to use some object in order to store your string <- that was already mentioned above.

Andriy Zhuk
  • 133
  • 6