Concatenate strings

This example will show how to concatenate strings in java. In addition to string concatenation java 8 introduced as part of the based JDK the class Joiner which allows the joining of two strings.

Concatenate w/ + operator

@Test
public void concatenate_strings_java () {
    
    String one = "one";
    String two = "two";
    
    assertEquals("onetwo", one + two);
}

Concatenate w/ string.concat

@Test
public void concatenate_strings_with_concat_java () {

    String one = "one";
    String two = "two";

    assertEquals("onetwo", one.concat(two)); 
}

Concatenate w/ stringbuilder

@Test
public void concatenate_strings_with_stringbuilder_java () {
    
    StringBuilder sb = new StringBuilder();
    sb.append("one");
    sb.append("two");
    
    assertEquals("onetwo", sb.toString());
}

Concatenate w/ stringbuffer

@Test
public void concatenate_strings_with_stringbuffer_java () {

    StringBuffer sb = new StringBuffer();
    sb.append("one");
    sb.append("two");
    
    assertEquals("onetwo", sb.toString());
}