Tutorial

Convert char to String in Java

Published on August 4, 2022
author

Pankaj

Convert char to String in Java

Sometimes we have to convert char to String in java program. Here we will look into different methods you can convert character to string in java. We will also learn how to convert char array to String using different methods.

Convert char to String Java

convert char to string java Here is a simple program showing different ways to convert char to string in java.

package com.journaldev.string;

public class CharToStringJava {

	public static void main(String[] args) {

		// char to string
		char c = 'a';
		String str = String.valueOf(c);

		// using Character class
		str = Character.toString(c);

		// another way
		str = new Character(c).toString();
		// string concatenation - worst performance
		str = "" + c;
		
		// char array to string
		char[] ca = { 'a', 'b', 'c' };
		str = String.valueOf(ca);
		// recommended way
		str = new String(ca);

	}

}

Let’s look at these methods one by one.

String.valueOf(char c)

This is the most efficient method to convert char to string. You should always use this method and this is the recommended way to convert character to string in java program.

Character.toString©

This method internally calls String.valueOf(c), so there is no difference between this one. You can use this too if you are already using Character class in your code.

new Character©.toString();

This is another way, however not recommended because we are creating a Character unnecessarily.

String concatenation

str = "" + c; is the worst way to convert char to string because internally it’s done by new StringBuilder().append("").append(c).toString() that is slow in performance. Let’s look at the two methods to convert char array to string in java program.

String constructor

You can use String(char[] value) constructor to convert char array to string. This is the recommended way.

String.valueOf(char[] data)

String valueOf method is overloaded and there is one that accepts character array. Internally this method calls the String constructor, so it’s same as above method. That’s all for converting char to string and char array to string in java.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 18, 2019

Hi Pankaj, Code below prints incorrect value while doing string processing. How do we convert int[] to String . int[] is converted to by stream processing. public class StringFilter { public static void main(String[] args) { String test= “abcd”; int[] array = test.chars().toArray(); System.out.println(String.valueOf(array)); } }

- Yogesh

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 31, 2016

    Nice article…www.marpasys.com

    - M A

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      October 25, 2016

      Thanks Pankaj

      - ajay

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Become a contributor for community

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        DigitalOcean Documentation

        Full documentation for every DigitalOcean product.

        Resources for startups and SMBs

        The Wave has everything you need to know about building a business, from raising funding to marketing your product.

        Get our newsletter

        Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

        New accounts only. By submitting your email you agree to our Privacy Policy

        The developer cloud

        Scale up as you grow — whether you're running one virtual machine or ten thousand.

        Get started for free

        Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

        *This promotional offer applies to new accounts only.