×

Search anything:

Convert String to char array in Java [3 methods]

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article at OpenGenus, we will see 3 different ways to convert a String to Char Array in Java Programming Language.

Table of contents:

  1. String in Java
  2. Char array in Java
  3. Difference between String and char in Java
  4. Need of conversion from string to an char array
  5. String to Char Array

First, we will take a look at basic concepts like String and Char array and also the need to convert String to Char array.

String in Java

String in Java is an object that represent sequence of character. String class is automatically imported as it belongs to java.lang package.

//Using New keyword
String s = new String("OpenGenus");
//Using String Literal
String s = "OpenGenus";

Char array in Java

Char is a primitive datatype in java. Char Array is a data structure which can store a Fixed number of characters.

char carray[] = new char;

Inputs can be taken using index in array

char [] carray = {'A','B','C'};

Array can also be declared and initialized at the same time.

Difference between String and char in Java

String and char array are different on several parameters. Some are Discussed below-

  • String in java is a class, whose object is created every time a string is declared whereas char array is a sequential collection of char datatype.
  • String in java are immutable i.e whenever a change is applied to a string in java , a new String object is created. Char array are mutable.
  • Built-in function like concat, charAt(),equals() can be used on java string. Char being a datatype does not provide built-in function.

Need of conversion from string to an char array

  1. String manipulation- Manipulation of individual characters in a string like replacing a character in char array is more efficient than in String functions.
String message = "Mello, world!";
char[] chars = message.toCharArray();
chars[0] = 'H';
String modifiedMessage = new String(chars);
System.out.println(modifiedMessage);

  1. Performance- Several operations over String are more efficiently performed if they were in char array. For Example- Like iterations over large array.
String largeString = "This is a very large string...";
char[] chars = largeString.toCharArray(); //
for (int i = 0; i < chars.length; i++) {
   // Perform some operation on the char array
}

  1. Interoperability- Some APIs and libraries may require char input/output. hence, conversion becomes necessary.For example java.util.Scanner.

String to Char Array

There are 3 different ways to convert a String to a char array:

  1. Using Built-In function toChharArray()
  2. Using For loop and charAt()
  3. Using getBytes method

We will dive into each method.

  1. Using Built-In function toChharArray()-

The 'toCharArray()' method is provide by the String class in java. It inputs an String object and return a char array representation of the string. This is the simplest way to convert string to char array as it does not involve looping.

String s = "Hello World"
char[] charArray = str.toCharArray();
  1. Using For loop and charAt()-

Another way to convert a String to a char array is to use a for loop to iterate over all the character in the string using charAt() and add it to a char array. This Approach is usually used when some other operation are to be performed on the string during conversion.

String s = "Hello World";
char[] charArray = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
    charArray[i] = str.charAt(i);
}
  1. Using getBytes method-

This methods is also provided by string class in java. It first converts the string to byte array which can further be converted into char array. This method is rather roundabout and mat not give correct result everytime as the string may contain non-ASCII character sets.

String s = "Hello World"
byte[] byteArray = str.getBytes();
char[] charArray = new String(byteArray).toCharArray();

The first two methods are more commonly used in practice.

With this article at OpenGenus, you must have the complete idea of how to convert a string to char array in Java.

Convert String to char array in Java [3 methods]
Share this