Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
String toCharArray() Function in Java
2.1.
Syntax
2.2.
Parameter
2.3.
Return Value
2.4.
Internal Implementation
2.5.
Examples
3.
Implementation
4.
Frequently Asked Questions
4.1.
What is the purpose of the String toCharArray function in Java?
4.2.
What is String in Java?
4.3.
What is the return type of String toCharArray() in Java?
5.
Conclusion
Last Updated: Mar 27, 2024

String toCharArray() in Java

Author Anju Jaiswal
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

When it comes to programming, strings are the most basic data structures. A character list, or more precisely, an "array of characters," is what they're made up of. With the help of an example, we will learn how to use the function string toCharArray() in this article.

String toCharArray() Function in Java

The toCharArray() method in Java is a built-in function that converts a string to a sequence of characters. The length of the output array is equal to the length of the String, and the chars in Array are in the same order as the characters in the String.

Syntax

The syntax of string toCharArray() method is given below:

string.toCharArray()

 

The string is a String class object in this case.

Parameter

The toCharArray() method doesn't take any parameters.

Return Value

It returns a newly allocated character array.

Internal Implementation

public char[] toCharArray() {  
      char output[] = new char[value.length];  
      System.arraycopy(value, 0, output, 0, value.length);  
      return output;  
  }

Examples

Input: hello !

Output: hello !

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Implementation

import java.util.*;
import java.io.*;
import java.lang.*;


class CodingNinjas
{
    public static void main (String[] args) throws Java.lang.Exception
    {
      String str = new String("Hello !");
/*String toCharArray() converts a string to a sequence of characters */

      char[] arr = str.toCharArray();

      System.out.print("Return Value : " );
      System.out.println(str.toCharArray() );
      System.out.println("\nSize of char[] = " + arr.length);
  

    }
}

 

Output:

Return Value : Hello !

Size of char[] = 7

 

Check out this problem - Shortest Common Supersequence.

Must Read Array of Objects in Java

Frequently Asked Questions

What is the purpose of the String toCharArray function in Java?

The toCharArray() function in Java transforms a string into a sequence of characters. The length of the output array is the same as the length of the String.

What is String in Java?

The String is a Java class found in the Java.lang package. It isn't a primary data type like int or long. String is a sequence of characters. Strings in Java are immutable, and the JVM stores all String objects in a String Pool.

What is the return type of String toCharArray() in Java?

It returns a newly allocated character array.

Conclusion

This article taught us about String toCharArray() in Java and its implementation. The Java String toCharArray() method converts the String to a char array and returns it. We have also seen some examples to understand better how this method works.

Recommended Readings:

Recommended problems -

 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!!

Live masterclass