Site icon Learn Automation

How To Convert Char to String In Java

How to convert char to String

In this tutorial, we will learn what is char array, How to convert char to string, How to convert char to int and String to char using different methods.

Let’s get started-

What is character array in Java?

It is a sequence of characters or array of characters which is used to store a piece of text. It is faster than lists and sets.

How to Convert Char to String

We can convert char into string using method valueOf() and toString().

Method1: Using toString()

public class ChartoString {

  public static void main(String[] args) {

    //input char variable 
    char c = 'A';

    //we will use toString() method 
    //toString method take character parameter and convert into string.

    String s= Character.toString(c);
    //printing string value
    System.out.println("String is: " + s);
  }
}

Output:

String is: c

Explanation of main points:

  1. Store a character into char variable c.
  2. Character– It is a wrapper class in Java
  3. toString()- To convert an object into string in java

Method2: Using valueOf()

public class ChartoString2{
  
public static void main(String args[]){ 
 
char c='K';  
String s1=String.valueOf(c);  
System.out.println("String is: "+s1); 
 
}
}  

Output:

String is: K

Explanation of main points:

  1. Store a character into char variable c.
  2. valueOf ()- To convert different types of object into string.

Related Programs:

How to convert char to int in Java

We can convert char into int using various methods.

Below are the different programs which covert char to int.

Method1: Using getNumericValue() method

public class CharToInt{  

public static void main(String args[]){  

char c1='5';  
int a=Character.getNumericValue(c1);  
System.out.println(“Integer value is:” + a);  
}
}  

Output:

Integer value is: 5

Explanation of main points:

  1. Store an int value into char variable c.
  2. getNumericValue()- It is a method to convert char to int.

Method2: Using String.valueOf() and parseInt().

In this program, first we will convert char into string using valueOf() method. Then using parseInt(), we convert string into integer value.

public class CharToIntExp{
  
public static void main(String args[]){  

char c='1';  
String s = String.valueOf(c);  
int a=Integer.parseInt(s); 
 
System.out.println(a);  
}
}  

Output:

1

Explanation of main points:

  1. Store an integer value into char variable c.
  2. Converted char into String using valueOf().
  3. Converted string into integer using parseInt().

Are you enjoying this post?

Let’s see few questions about string and char conversions.

Q1. Can you convert String to char?

Yes. We can convert string into char using charAt() method of String class. It is always return single char.

Q2. Can a char be a string?

No, String can contain nothing

String s=””;  // It is a string

In Java, string is an object.

Char is a primitive type in Java.

String to char Conversion in Java

Sometimes we get the values in string format in java. Printing the value of this string is no problem but if I want to use this string in the form of char then we need to convert it into char.

For example, a char value obtain in string format in command line argument or in getText() method.

In java, we can convert string into char using String class method- charAt(). Other way using substring() method of String class.

Conversion of string to char

Program using chatAt() method: –

public class StrtoChar
{
  public static void main(String args[])
  {

    String str1 = "A";
    System.out.println("A in String form: " + str1);           
    char ch1 = str1.charAt(0);                               // convert string to char
    System.out.println("A in char form: " + ch1);
         
  }
}

Output:

A in String form: A

A in char form: A

Explanation:

charAt(0) is a method of string class which return the char at index 0 in the string str1.

Summary:

In this post, we have covered “How to convert char to String”,Convert char to int, Convert String to char by using different methods like toString(), valueOf(), charAt() and parseInt().

I am sure this content added some additional value in your skills and also helpful to preparation of your interviews.

Final word, Bookmark this post “How to convert char to String” for future reference.

In future, I will add more java programming in this blog.

Must read below post: –

Palindrome Number in Java

Armstrong Number in Java

Exit mobile version