Open In App

Difference between String and Character array in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 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);

Last Updated : 05 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads