Char number to string java

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.

Читайте также:  Selenium find text on page python

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:

6 ways to convert a char to 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

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

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» .

Java char to String conversion example

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.

Java convert char to String example

Output:

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.

Youtube

For Videos Join Our Youtube Channel: Join Now

Feedback

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

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 RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью