[BAEL-7048] - Passing Strings By Reference in Java (#15047)
* feat: pass by reference java string * fix: review
This commit is contained in:
parent
a1698c6b83
commit
c65981938f
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.passstringbyreference;
|
||||||
|
|
||||||
|
public class Dummy {
|
||||||
|
|
||||||
|
String dummyString;
|
||||||
|
|
||||||
|
public String getDummyString() {
|
||||||
|
return dummyString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDummyString(String dummyString) {
|
||||||
|
this.dummyString = dummyString;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
package com.baeldung.passstringbyreference;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class PassStringUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAString_whenPassedToVoidMethod_thenStringIsNotModified() {
|
||||||
|
String s = "hello";
|
||||||
|
concatStringWithNoReturn(s);
|
||||||
|
assertEquals("hello", s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void concatStringWithNoReturn(String input) {
|
||||||
|
input += " world";
|
||||||
|
assertEquals("hello world", input);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAString_whenPassedToMethodAndReturnNewString_thenStringIsModified() {
|
||||||
|
String s = "hello";
|
||||||
|
assertEquals("hello world", concatStringWithReturn(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
String concatStringWithReturn(String input) {
|
||||||
|
return input + " world";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAString_whenPassStringBuilderToVoidMethod_thenConcatNewStringOk() {
|
||||||
|
StringBuilder builder = new StringBuilder("hello");
|
||||||
|
concatWithStringBuilder(builder);
|
||||||
|
|
||||||
|
assertEquals("hello world", builder.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
void concatWithStringBuilder(StringBuilder input) {
|
||||||
|
input.append(" world");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAString_whenPassStringBufferToVoidMethod_thenConcatNewStringOk() {
|
||||||
|
StringBuffer builder = new StringBuffer("hello");
|
||||||
|
concatWithStringBuffer(builder);
|
||||||
|
|
||||||
|
assertEquals("hello world", builder.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
void concatWithStringBuffer(StringBuffer input) {
|
||||||
|
input.append(" world");
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user