Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Sometimes, we might want to pass and modify a String within a method in Java. This happens, for example, when we want to append another String to the one in the input. However, input variables have their scope inside a method. Furthermore, a String is immutable. Therefore, finding a solution is unclear if we don’t understand Java memory management.

In this tutorial, we’ll understand how an input String is passed to a method. We’ll see how we can use a StringBuilder and how to preserve immutability by creating new objects.

2. Pass by Value or Reference

Being an OOP language, Java can define primitives and objects. They can be stored in the stack or heap memory. Furthermore, they can be passed by value or reference to a method.

2.1. Objects and Primitives

Primitives have allocation in the stack memory. When passed to a method, we get a copy of the primitive’s value.

Objects are instances of class templates. They are stored in the heap memory. However, inside a method, a program can access them because it has a reference to the address in the heap memory. Similarly to a primitive, when passing an object to a method, we get a copy of the object’s reference (which we can think of as a pointer).

Although there is a difference between passing a primitive or an object, variables or objects have their scope inside a method. In both cases, a call-by-sharing is what is happening, and we can’t directly update the original value or reference. Therefore, parameters are always copied.

2.2. String Immutability

A String is a class instead of a primitive in Java. Therefore, given its runtime instance, we’ll get a reference when passing it to a method.

Additionally, it’s immutable. Therefore, even if we want to manipulate the String within a method, we can’t modify it unless we create a new one.

3. Use Case

Let’s define a primary use case before digging into a general solution to the problem.

Suppose we want to append to an input String within a method. Let’s test what happens before and after the method execution:

@Test
void givenAString_whenPassedToVoidMethod_thenStringIsNotModified() {
    String s = "hello";
    concatStringWithNoReturn(s);
    assertEquals("hello", s);
}

void concatStringWithNoReturn(String input) {
    input += " world";
    assertEquals("hello world", input);
}

The String gets a new value inside the concatStringWithNoReturn() method. However, we still have the original value outside the method’s scope.

Naturally, a logical solution would be to make a method return a new String:

@Test
void givenAString_whenPassedToMethodAndReturnNewString_thenStringIsModified() {
    String s = "hello";
    assertEquals("hello world", concatString(s));
}

String concatStringWithReturn(String input) {
    return input + " world";
}

Notably, we avoid side effects while safely returning a new instance.

4. Use a StringBuilder or StringBuffer

Although String concatenation is an option, using a StringBuilder (or StringBuffer as the thread-safe version) is a better practice.

4.1. StringBuilder

@Test
void givenAString_whenPassStringBuilderToVoidMethod_thenConcatNewStringOk() {
    StringBuilder builder = new StringBuilder("hello");
    concatWithStringBuilder(builder);

    assertEquals("hello world", builder.toString());
}

void concatWithStringBuilder(StringBuilder input) {
    input.append(" world");
}

The String we append to the builder is temporarily stored in an array of characters. Therefore, comparing this approach with a String concatenation, the main benefit is performance-wise. Thus, we won’t create a new String every time. Instead, we wait until we have the sequence we want and, at that moment, make the required String.

4.2. StringBuffer

We also have the thread-safe version, the StringBuffer. Let’s also see this in action:

@Test
void givenAString_whenPassStringBufferToVoidMethod_thenConcatNewStringOk() {
    StringBuffer builder = new StringBuffer("hello");
    concatWithStringBuffer(builder);

    assertEquals("hello world", builder.toString());
}

void concatWithStringBuffer(StringBuffer input) {
    input.append(" world");
}

If we need synchronization, this is the class we want. Naturally, this can slow down the process, so let’s understand first if it’s worth it.

5. Working With Objects Properties

What if a String is an object property?

Let’s define a simple class we can use for testing:

public class Dummy {

    String dummyString;
    // getter and setter
}

5.1. Modify String State With the Setter

At first, we could think of simply using the setter to modify the object’s String state:

@Test
void givenObjectWithStringField_whenSetDifferentValue_thenObjectIsModified() {
    Dummy dummy = new Dummy();
    assertNull(dummy.getDummyString());
    modifyStringValueInInputObject(dummy, "hello world");
    assertEquals("hello world", dummy.getDummyString());
}

void modifyStringValueInInputObject(Dummy dummy, String dummyString) {
    dummy.setDummyString(dummyString);
}

Notably, we’ll update a copy of the original object in the heap memory (still pointing to the actual value).

However, this isn’t a good practice. It hides the String change. Furthermore, we can have synchronization issues if multiple threads are trying to modify the object.

Overall, whenever possible, we should look for immutability and make a method return a new object.

5.2. Create a New Object

It’s good practice to make methods return new objects when applying some business logic. Furthermore, we can also set properties using the StringBuilder pattern we have seen earlier. Let’s wrap this up in a test case:

@Test
void givenObjectWithStringField_whenSetDifferentValueWithStringBuilder_thenSetStringInNewObject() {
    assertEquals("hello world", getDummy("hello", "world").getDummyString());
}

Dummy getDummy(String hello, String world) {
    StringBuilder builder = new StringBuilder();

    builder.append(hello)
      .append(" ")
      .append(world);

    Dummy dummy = new Dummy();
    dummy.setDummyString(builder.toString());

    return dummy;
}

Although this is a simplified example, we can see how the code is more readable. Furthermore, we avoid side effects and maintain immutability. Any input information of a method is something we use to construct a well-identified instance of a new object.

6. Conclusion

In this article, we saw how to change a method’s input String while preserving immutability and avoiding side effects. We saw how to use a StringBuilder and apply this pattern in new object creation.

As always, the code presented in this article is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.