Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

String is a common type, and char is a primitive in Java.

In this tutorial, we’ll explore how to convert a String object to char in Java.

2. Introduction to the Problem

We know that a char can only contain one single character. However, a String object can contain multiple characters.

Therefore, our tutorial will cover two cases:

  • The source is a single-character string.
  • The source is a multi-character string.

For case 1, we can get the single character as a char easily. For example, let’s say this is our input:

String STRING_b = "b";

After the conversion, we expect to have a char ‘b‘.

For case 2, if the source String is a multi-character string and we still want to get one single char as a result, we must analyze the requirement to pick the desired character, such as the first, the last, or the n-th character.

In this tutorial, we’ll address a more general solution. We’ll convert the source String to a char array that holds each character in the string. In this way, we can pick any elements according to the requirement.

We’ll use STRING_Baeldung as the input example:

String STRING_Baeldung = "Baeldung";

So next, let’s see them in action.

3. Single Character String

Java’s String class provides the charAt() to get the n-th character (0-based) from the input string as char. Therefore, we can directly call the method getChar(0) to convert a single character String to a char:

assertEquals('b', STRING_b.charAt(0));

However, we should note that if the input is an empty string, the charAt() method call throws StringIndexOutOfBoundsException:

assertThrows(StringIndexOutOfBoundsException.class, () -> "".charAt(0));

Therefore, we should check the input string is not null or empty before we call the charAt() method.

4. Multiple Characters String

We’ve learned to use charAt(0) to convert a single character String to char. If the input is a multi-character String, and we know exactly which character we want to be converted to a char, we can still use the charAt() method. For example, we can get the fourth character (‘l‘) from the input string “Baeldung“:

assertEquals('l', STRING_Baeldung.charAt(3));

Additionally, we can use String.toCharArray() to get a char[] array containing all the characters:

assertArrayEquals(new char[] { 'B', 'a', 'e', 'l', 'd', 'u', 'n', 'g' }, STRING_Baeldung.toCharArray());

It’s worth mentioning that the toCharArray() method also works for empty string inputs. It returns an empty char array as the result:

assertArrayEquals(new char[] {}, "".toCharArray());

Apart from toCharArray(), String.getChars() can extract consecutive characters from the given String to a char array. The method receives four arguments:

  • srcBegin – the index of the first character in the string to take, inclusive
  • srcEnd – the index of the last character in the string to copy, exclusive
  • dst – the destination array, which is our result
  • dstBegin – the start offset in the destination array. We’ll discuss this with an example.

First, let’s extract “aeld” from the string “Baeldung” and fill it into a predefined char array:

char[] aeld = new char[4];
STRING_Baeldung.getChars(1, 5, aeld, 0);
assertArrayEquals(new char[] { 'a', 'e', 'l', 'd' }, aeld);

As the test above shows, to call getChars(), we should first have a char array to hold the result.

In the example, we pass 0 for dstBegin when we call getChars(). This is because we’d like the converted result to start from the first element in the array aeld.

Of course, sometimes, we want the result to overwrite a middle part of the array. Then we can set the dstBegin to the desired value.

Next, let’s see another example to convert “aeld” to chars and overwrite the target array from the second (index=1) element:

char[] anotherArray = new char[] { '#', '#', '#', '#', '#', '#' };
STRING_Baeldung.getChars(1, 5, anotherArray, 1);
assertArrayEquals(new char[] { '#', 'a', 'e', 'l', 'd', '#' }, anotherArray);

So, as we can see, we pass dstBegin=1 to the method and get the expected result.

5. Conclusion

In this article, we’ve learned how to convert a String to a char in Java.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW