Difference between String and Character array in Java
Last Updated :
25 Jul, 2024
Improve
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:
Property | String | Character Array |
---|---|---|
Definition | A sequence of characters is represented as a single data type. | A sequential collection of characters of the data type char. |
Mutability | Immutable – Once created, their values cannot be changed. | Mutable – Individual characters can be modified. |
Built-in Functions | Provides many built-in functions like substring(), charAt(), length(), etc. | No built-in functions for operations. |
Concatenation | Strings can be concatenated using the + operator. | Cannot use + for concatenation; must use loops or utility methods. |
Access Characters | Characters can be accessed using the charAt() method. | Characters can be accessed using array indexing ([]). |
Memory Storage | Can be stored anywhere in memory, typically managed by the JVM. | Elements are stored contiguously in increasing memory locations. |
String Constant Pool | Strings are stored in String Constant Pool if they are string literals or interned, otherwise they are stored in the heap. | Stored in the Heap. |
Security | Not preferred for storing passwords due to immutability and persistence in the String Pool. | Preferred for storing passwords as they can be explicitly cleared after use. |
Conversion | Can be converted to a Character Array using the toCharArray() method. <br> Example: String s = “GEEKS”; char[] ch = s.toCharArray(); | Can be converted to a String using the String constructor. <br> Example: char[] a = {‘G’, ‘E’, ‘E’, ‘K’, ‘S’}; String A = new String(a); |
Key Differences:
- Immutability vs. Mutability:
- String: Immutable, meaning once a String object is created, it cannot be altered. This property makes them suitable for use as keys in hashmaps or for string manipulation without side effects.
- Character Array: Mutable, allowing changes to individual characters, making them more suitable for scenarios where frequent modifications are required, like password storage.
- Built-in Functions:
- String: Java provides a rich set of built-in methods for various operations, including searching, splitting, and replacing substrings.
- Character Array: Lacks built-in methods, so manual handling or utility methods are required for similar operations.
- Memory Management:
- String: Stored in the String Constant Pool when it is defined as literal, in double quotes – “Hello, World!”. This allows for reuse of common strings, thus saving memory.
If it is created by using new keyword via constructors new String(“Hello, World!”) they are stored in normal heap area. - Character Array: Stored in the heap, providing direct and contiguous access to memory locations.
- String: Stored in the String Constant Pool when it is defined as literal, in double quotes – “Hello, World!”. This allows for reuse of common strings, thus saving memory.
- Security Considerations:
- String: Due to immutability and persistence in the String Pool, they can be a security risk for sensitive data like passwords.
- Character Array: Since they can be explicitly modified and cleared, they are preferred for storing sensitive information.