Difference between String and Character array in Java
Unlike C/C++ Character arrays and Strings are two different things in Java. Both Character Arrays and Strings are a collection of characters but are different in terms of properties.
Differences between Strings and Character Arrays:
| Strings | Character Arrays |
|---|---|
| String refers to a sequence of characters represented as a single data type. | Character Array is a sequential collection of data type char. |
| Strings are immutable. | Character Arrays are mutable. |
| Built in functions like substring(), charAt() etc can be used on Strings. | No built in functions are provided in Java for operations on Character Arrays. |
| ‘+’ can be used to appended strings together to form a new string. | ‘+’ cannot be used to append two Character Arrays. |
| The charAt() method can be used to access characters at a particular index in a String. | The characters in a Character Array can be accessed normally like in any other language by using []. |
| Strings can be stored in any any manner in the memory. | Elements in Character Array are stored contiguously in increasing memory locations. |
| All Strings are stored in the String Constant Pool. | All Character Arrays are stored in the Heap. |
| Not preferred for storing passwords in Java. | Preferred for storing passwords in Java. |
| A String can be converted into Character Array by using the toCharArray() method of String class. Eg: String s = “GEEKS”; char [] ch = s.toCharArray(); |
A Character Array can be converted into String by passing it into a String Constructor. Eg: char[] a = {‘G’, ‘E’, ‘E’, ‘K’, ‘S’}; String A = new String(a); |
Recommended Posts:
- Convert a String to Character array in Java
- Difference between Optical Character Recognition (OCR) and Magnetic Ink Character Reader (MICR)
- How to get character array from string in JavaScript?
- Java Program to get a character from a String
- Count occurrence of a given character in a string using Stream API in Java
- Replace a character at a specific index in a String in Java
- Java program to count the occurrence of each character in a string using Hashmap
- Java Program to convert Character Array to IntStream
- Convert an ArrayList of String to a String array in Java
- Convert Set of String to Array of String in Java
- Difference between length of Array and size of ArrayList in Java
- Why to use char[] array over a string for storing passwords in Java?
- How to remove the first character of string in PHP?
- How to get the last character of a string in JavaScript?
- How to remove a character from string in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



