1. 使用String.valueOf()方法

@Test
public void givenChar_whenCallingStringValueOf_shouldConvertToString() {
char givenChar = 'x';

String result = String.valueOf(givenChar);

assertThat(result).isEqualTo("x");
}

2. 使用 Character.toString() 方法

@Test
public void givenChar_whenCallingToStringOnCharacter_shouldConvertToString() {
char givenChar = 'x';

String result = Character.toString(givenChar);

assertThat(result).isEqualTo("x");
}

3. 使用Character的构造方法

@Test
public void givenChar_whenCallingCharacterConstructor_shouldConvertToString() {
char givenChar = 'x';

String result = new Character(givenChar).toString();

assertThat(result).isEqualTo("x");
}

4. 通过String的隐式转换

@Test
public void givenChar_whenConcatenated_shouldConvertToString() {
char givenChar = 'x';

String result = givenChar + "";

assertThat(result).isEqualTo("x");
}

5.

@Test
public void givenChar_whenFormated_shouldConvertToString() {
char givenChar = 'x';

String result = String.format("%c", givenChar);

assertThat(result).isEqualTo("x");
}

总结: 常用Stirng的隐式转换方式.

本文总阅读量