How to convert Char to String in Java

  • 23 November 2020
  • ADM

 

How to convert Char to String in Java - images/logos/java.jpg

 

To convert char to String in Java there are two simple options:

  • using String.valueOf
  • using Character.toString

Example

package com.admfactory;

public class CharToString {

    public static void main(String[] args) {
	char a1 = 'a';
	char a2 = 'b';

	String str0 = Character.toString(a1);

	String str1 = String.valueOf(a2);

	System.out.println("char to String example.");
	System.out.println();
	System.out.println("a1: " + a1);
	System.out.println("a2: " + a2);
	System.out.println("Character.toString: " + str0);
	System.out.println("String.valueOf: " + str1);
    }
}

Output

char to String example.

a1: a
a2: b
Character.toString: a
String.valueOf: b

 

References