Strings and characters are used to store and manipulate text data in Java. In this post, we will explain what these two data types are, how they differ from each other, and when to use them in our code.

1. Overview of String

A String object represents a sequence of characters as a single data type. We can create a String object using a literal, which is a sequence of characters enclosed in double quotes, or using the new keyword and passing a literal or a character array as an argument to the String constructor. Strings are immutable in Java, which means that their value cannot be changed once they are created. Any operation that modifies a String, such as concatenation, substring, replace, etc., will create a new String object rather than modifying the original one.

They are stored in a special memory area called the String constant pool, which is a part of the heap memory. The String constant pool stores all the String literals that are used in the program, and it avoids creating duplicate objects for the same literal. If we have two variables that refer to the same literal, they will point to the same object in the pool. However, if we create a String object using the new keyword, it will not be stored in the pool, but in the regular heap memory. This means that even if we create two objects with the same value using the new keyword, they will not be equal.

Strings in Java also have many built-in methods that allow us to perform various operations on them, such as finding the length, getting a character at a specific index, extracting a substring, converting to upper or lower case, trimming whitespace, splitting by a delimiter, joining by a delimiter, comparing with another string, searching for a pattern, replacing a part of the string, formatting with placeholders, etc.

2. Overview of Character Array

A character array is an array of primitive data type char, which represents a single character. A character array can be created using either literals or constructors. A character array is mutable, which means that its elements can be changed after it is created.

A character array is similar to any other array in Java, which means that it has a fixed size that cannot be changed once it is created, it can be accessed or modified using an index (starting from 0), it can be iterated using a loop, it can be passed as an argument to a method or returned from a method, etc.

A character array does not have any built-in methods that can be used on it. It has the length attribute to find the length of a character array, which is inherited from the array class. To perform any operation on a character array, we have to write our own code or use some external libraries.

3. Differences between String and Character Array

Now that we have seen what String and Character array are in Java, let us summarize some of their key differences:

  • A string is immutable in Java, while a character array is mutable. This means that we cannot change the contents of a string object once it is created. Any operation that results in the modification of data will result in a new String.
  • All string literals are stored in the string constant pool. They remain in the pool until they became eligible for the garbage collection, while a Character array is stored in heap memory.
  • The String class has several useful methods to operate on a string instance such as contains, equals, length, trim, indexOf, replace, substring, etc. In contrast, a character array doesn’t have any such methods which operate on its values. The character array only offers the length property.
  • We cannot iterate over the characters of a string using an enhanced for-loop. On the other hand, we can iterate over the character array using enhanced for-loop (for-each loop).
  • To access a specific character in a character array, we pass its index to the [] operator. To access a specific character in a string, we can pass its index to its charAt() method.
  • A Character array should be preferred over a string for storing passwords in Java. The immutable property of string makes it vulnerable for storing passwords as we can’t get rid of the password after usage until the garbage collector collects it. On the other hand, a Character array is mutable and can be cleared immediately after use before the garbage collection kicks in.
  • The addition (+) operator is overloaded for the String class, and we can use it to concatenate two strings. Note that since string is immutable, the String concatenation operator will produce a new String.

4. Conversion between String and Character array

Sometimes, we may need to convert between string and character array in Java, depending on our requirements. Java provides some easy ways to do this.

 
To convert a string to a character array, we can use the toCharArray() method of the String class. This method returns a copy of the underlying character array of the string. For example:

 
If you need to convert a string to a Character[] instead, we can use Stream API.

 
To convert a character array into a string, we can use the constructor of the String class that takes a character array as an argument. This constructor creates a new string object with the same characters as the array. We can even encapsulate the String constructor call by using valueOf() or copyValueOf() method of the String class. For example:

5. When to use String and Character array

Both string and character array have their own advantages and disadvantages in Java. We should choose one over the other depending on our situation and needs. Some general guidelines are:

  • Use strings when we need to store and manipulate text data that does not change frequently.
  • Use strings when we need to perform various operations on text data using built-in methods.
  • Use strings when we need to compare or concatenate text data using operators or methods.
  • Use character arrays when we need to store and manipulate text data that changes frequently.
  • Use character arrays when we need to access or modify individual characters using index notation.
  • Use character arrays when we need to save memory space and improve performance by avoiding creating new objects.

That’s all about the differences between String and Character array in Java.