- 6 ways to convert char to String in Java — Examples
- 1.Char to String using concatenation in Java
- 2. Character to String using String.valueOf()
- 3. Char to String conversion with Character.toString()
- 4. Char to String using a Character wrapper class
- String constructor with a char array
- char to String using String.valueOf(char[])
- Convert char to String in Java
- Convert char to String Java
- String.valueOf(char c)
- Character.toString©
- new Character©.toString();
- String concatenation
- String constructor
- String.valueOf(char[] data)
- Java Convert char to String with examples
- Java char to String example using String.valueOf(char ch)
- Java char to String conversion using Character.toString(char ch)
- Top Related Articles:
- About the Author
- Char number to string java
- Java char to String Example: Character.toString() method
- Feedback
- Help Others, Please Share
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
6 ways to convert char to String in Java — Examples
If you have a char value like ‘a’ and you want to convert it into equivalent String like «a» then you can use any of the following 6 methods to convert a primitive char value into String in Java :
1) String concatenation
2) String.valueOf()
3) Character.toString()
4) Character wrapper class + toString
5) String constructor with a char array
6) String.valueOf(char [])
In this article, we will see examples of each approach and learn a little bit more about it. Actually, there is a lot of overlap between each method as some of them internally call String.valueOf() , which eventually calls to a String constructor which accepts char array and creates a String object containing primitive char value with length 1. Once you know, how they work internally, it easy to decide which one is more efficient for the purpose.
1.Char to String using concatenation in Java
This is the easiest way to convert a char to a String object in Java. All you need to do is concatenate a given character with an empty String, as shown in the following example:
char apple = 'a'; String aStr = "" + apple; System.out.println("char to String using concatenation : " + aStr); // a
Remark: This is the simplest method to convert a char value to a String but it’s less efficient and takes more memory than required. Compile translate String concatenation into the following code:
new StringBuilder().append(x).append("").toString();
This is less efficient because the StringBuilder is backed by a char[] of size 16, which is a waste of memory if you just converting one character. This array is then defensively copied by the resulting String
2. Character to String using String.valueOf()
This is the standard way to convert a char primitive type into a String object. If you remember, we have already used the valueOf() method to convert String to int and String to double in Java. Here is the example of using this method to convert char to String:
char boy = 'b'; String bStr = String.valueOf(boy); System.out.println("char to String using String.valueOf() : " + bStr); // "b"
Remark: String.valueOf(char) is the most efficient method for converting char to String in Java because it just uses a character array of size one to wrap a given character and pass it to String constructor, as shown below (from JDK) :
3. Char to String conversion with Character.toString()
This is the third example to convert a char primitive value to a String object in Java. In this example, we have used Character.toString() method.
char cat = 'c'; String cStr = Character.toString(cat); System.out.println("char to String using Character.toString() : " + cStr);
Remark: This method returns a String object representing the given char. It returns a String of length one consisting solely of the specified char. Internally it also calls String.valueOf(char c) method.
Anyway, here is a nice summary of all the six ways to convert a char value to a String object in Java:
4. Char to String using a Character wrapper class
This is the 4th example of converting a char value to a String object in Java. This time, we will use java.lang.Character class, which is a wrapper for char primitive type.
char dog = 'd'; String dStr = new Character(dog).toString(); System.out.println("char to String using new Character() + toString : " + dStr);
Remark: This method returns the String equivalent of the Character object’s value of length 1. Internally this method wraps the encapsulated char value into a char array and passed it to the String.valueOf(char[]) method.
String constructor with a char array
Here is one more way to convert a char value to a String object in Java. In this example, we have wrapped the single character into a character array and passed it to a String constructor which accepts a char array as shown below:
String fStr = new String(new char[]'f'>); System.out.println("char to String using new String(char array) : " + fStr);
Remark: This is the method which is internally called by String.valueOf() method for char to String conversion. This constructor allocates a new String object to represent a sequence of characters passed via char array argument. The contents of the character array are defensively copied so that subsequent modification of the character array doesn’t affect the newly created String.
char to String using String.valueOf(char[])
This the sixth and last way to perform char to String conversion in our article, here is the sample code:
String gStr = String.valueOf(new char[]'g'>); System.out.println("char to String using String.valueOf(char array) : " + gStr);
Remark: String.valueOf() is overloaded to accept both char and char[] . Since the valueOf() method accepts a char, internally wrap that char into a character array it makes sense to use this overloaded method to directly pass a char array, which in turn passed to String constructor which accepts a character array.
That’s all about how to convert a char value to a String in Java. You can choose any of the above methods, which suits your test but remember, String concatenation is not efficient and can waste a lot of memories if used in the loop. String.valueOf() is more readable and efficient, so should be your default choice. All of the above methods internally use a new String(char[]) though.
Convert char to String in Java
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.
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
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); > >
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();
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 us
Java Convert char to String with examples
In this tutorial, we will see how to convert a char to string with the help of examples.
There are two ways you can do char to String conversion –
1. Using String.valueOf(char ch) method
2. Using Character.toString(char ch) method
Java char to String example using String.valueOf(char ch)
We can use the valueOf(char ch) method of String class to convert a passed char ch to a String. This method accepts char as an argument and returns the string equivalent of the argument.
In the following example we have a char ch with the value ‘P’ and we are converting this to a String str using the String.valueOf() method. The value of the String after conversion is «P» .
Output:
Java char to String conversion using Character.toString(char ch)
We can also use the toString(char ch) method of Character class to convert the passed char ch to a String. Similar to String.valueOf(char ch) method, this method also accepts char as a parameter and returns an equivalent String.
In the following example we are converting a char to String using Character.toString(char ch) method.
Output:
Top Related Articles:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
Char number to string java
Java char to String Example: Character.toString() method
Let’s see the simple code to convert char to String in java using Character.toString() method.
For Videos Join Our Youtube Channel: Join Now
Feedback
Help Others, Please Share
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter