How to Convert Characters to Strings in Java 

java char to stringVariables are memory location references used to store values. When you create a variable, you actually reserve a space in memory. A user decides what type of value has to be stored in the variable. For instance, a user creates input such as an integer, decimal or characters. Java, being a high level programming language, provides users with different data types to store different types of information. There are two data types:

  • Primitive data types
  • Reference/Object data types

Learn Java from scratch at Udemy.com

The primitive data type contains eight primitive data types used frequently in very basic to complex java program:

  • Byte
  • Short
  • Integer
  • Float
  • Double
  • Boolean
  • Character

Reference Type

These types are created using constructors of each class, and they help in accessing objects. These types are specific and cannot be changed. Classes, objects and arrays are included in these data types.

These data types store information based occupied memory. The character data type is used to store each alphabetic letter, punctuation and special characters. Numeric calculations are not possible with the values stored in the character data type. The character data type is a single 16-bit, 2 byte Unicode character.

You can declare a character data type using the following code:

char i_am_char;

char i_am_also_char;

With the variables defined, you can now assign them a value such as the following:

i_am_char=‘a’;

i_am_also_char=96;

A point to note in the second example is that the number gets replaced with the character’s ASCII value representation.

For a full-fledged class example, consider the following code:

class Chardef

{

public static void main (String[] args) throws java.lang.Exception

{

char ch1=‘a’;

char ch=97;

System.out.println(“Yes I am 1st character ” +ch1);

System.out.println(“Yes I am also character ” +ch);

}

}

Output:

Yes I am 1st character a

Yes I am also character a

Since the ASCII value of the alphabet ‘a’ is 97, the equivalent character is printed.

A String is a sequence or combination of characters. In Java, it is a Class. The String class is immutable, so it cannot be changed. StringBuffer is another class in Java used to store the String value, but it is mutable and can be changed using the append statement.

String str=“I am a String”;

StringBuffer str1=new StringBuffer(“I am the String Buffer”);

Find out more about Java programming through a course at Udemy.com

Difference between Character and String:

  • A character is a primitive data type in Java. A String is a class used to store a sequence or combination of characters into strings objects.
  • Interconversion from character to string and string to character.
  • The character has fixed memory allocation while String has no such memory restrictions.

A String can easily be converted to character array:

 

import java.util.*;

import java.lang.*;

import java.io.*;

 

class Stringtochar

{

public static void main (String[] args) throws java.lang.Exception

{

// Declaring a String

String str="I am a string ";

//String to char conversion

char [] ch=str.toCharArray();

System.out.println(“character Array is”);

System.out.println(ch);

}

}

Output:

character Array is

I am a string

The value of a string is passed to the toCharArray() and the string is converted into the character array that is printed on the screen.

Char To String Conversion:

You have a few methods you can use to perform a character to string conversion.

The first method is using plus operator (“+”). The following is an example:

class Chartostring

{

public static void main (String[] args) throws java.lang.Exception

{

Scanner sc=new Scanner(System.in);

// Enter a series of characters from the user

char [] ch=new char[15];

for(int x=0;x<15;x++)

{

ch[x]=sc.next().charAt(0); //store the characters entered by the user in a char array

}

String str="";

for (int x=0;x<15;x++)

{

str=str+ch[x];  //Character to string conversion using the plus operator

}

System.out.println(str);

}

}

Input:

a b c d e f g h i j k l m n o

Output:

Abcdefghijklmno

In the above program, every character is saved in the char array and then the character array is parsed. Every character is concatenated with the string object using the plus operator to get the final string.

The character class also has a method called toString(). It is a static method and takes a character as one argument. The passed character is then converted to a string.

Syntax:

char ch=”A”

String str=Character.toString(ch);

The character class also has function called valueOf(). It is a static method and takes character value as argument and returns the a string as output.

Syntax:

char ch=”B”;

String str=String.valueOf(ch);

In this method, the character is wrapped into the character array and the array is passed to the String constructor. The new string is created from the characters.

Syntax:

char ch=”C”

String str2 = new String(new char[]{ch});

Learn a lot more about Java by taking up this ultimate course at Udemy.com

The following example uses of all the above three functions to convert characters to a String.

import java.util.*;

import java.lang.*;

import java.io.*;

 

class CharToString

{

public static void main (String[] args) throws java.lang.Exception

{

// Saving the Series of Characters

char ch1='a';

char ch2='b';

char ch3='c';

char ch4='d';

//String declaration

String str,str2,str3;

//  Character to String conversion using ToString method

System.out.println("Using toString method");

str=Character.toString(ch1);

System.out.print(str);

str=Character.toString(ch2);

System.out.print(str);

str=Character.toString(ch3);

System.out.print(str);

str=Character.toString(ch4);

System.out.println(str);

//Character to String conversion by passing character array method

System.out.println("Using Character Array Passing");

str2= new String(new char[]{ch1});

System.out.print(str2);

str2= new String(new char[]{ch2});

System.out.print(str2);

str2= new String(new char[]{ch3});

System.out.print(str2);

str2= new String(new char[]{ch4});

System.out.println(str2);

//Character to String conversion using valueOf method

System.out.println("Using Valueof");

str3 = String.valueOf(ch1);

System.out.print(str3);

 

str3 = String.valueOf(ch2);

System.out.print(str3);

str3 = String.valueOf(ch3);

System.out.print(str3);

str3 = String.valueOf(ch4);

System.out.print(str3);

}

}

Output:

Using toString method

abcd

Using Character Array Passing

abcd

Using Valueof

abcd

In the above program, you used all the predefined java functions to convert the character values into the a string. Functions valueOf() and toString() take a character argument and return the string.

Another example is using the StringBuffer class. The following is an example using the StringBuffer class:

class StringBuffer

{

public static void main (String[] args) throws java.lang.Exception

{

Scanner sc=new Scanner(System.in);

// Enter the Series of Character

char ch1='a';

char ch2='b';

char ch3='c';

char ch4='d';

//Create object of String Buffer Class

StringBuffer str=new StringBuffer();

str.append(ch1); //append the character in the string buffer

str.append(ch2);

str.append(ch3);

str.append(ch4);

System.out.println(str);

}

}

The above program describes how to convert a character into a string using the string buffer class. The append method makes the class mutable and allows changes to the string.