Main Difference Between Array and String in Programming

Main Difference Between Array and String in Programming

Strings Vs Arrays

Strings and arrays have similar functionality with some differences. They are implemented differently in different programming languages.

The most common difference between array data structure and string is, the array can have any data type while strings are ASCII characters. These characters in strings are terminated with a null character ‘\0’.

Strings and arrays work differently in C \ C++ and in Java.

In this article, we see a detailed comparison between strings and arrays. Let’s first have a look at what are strings and arrays individually.

What is an Array?

Many applications have a very large amount of data. To process such volumes of data there is a need for a strong data type that is efficient in storing and accessing data items. Thus the concept of array came into the picture.

Technically, the array is a sequential collection of similar variables that share a common name. Read the array in detail.

In other words, it is a convenient way of grouping similar items.

The general syntax for an array declaration is:

<component type>  <variable identifier>[<integer value>];



The above is an example of a character array. Just like characters, you can also use arrays to store numeric values.

What is a String?

A string, a collection of characters represented as a single item and it is terminated with a null character ‘\0’.

The declaration of the string is similar to the array but with an exception of char type. The string is considered as a contiguous sequence of values with a common name.

The string is immutable that means the values in it cannot be changed once they are assigned.


string string_name;


arr[]={‘hello’, ‘good’ ,‘day’, ‘please’}

The above is an example of the string array.

Difference Between Array and String

Array String
The array is a collection of variables that are of the same data type. The string can be described as a class and the variables of it are the object of class “string”.
A character in a character array can be accessed by its index in an array. A particular character can be accessed by the function “string_name.charAt(index)”.
It does not define an implicit datatype. In C++, string defines a datatype.
Operators cannot be applied to character array in C++. A standard C++ operator on the string can be applied in C++
Array boundaries are easily overrun. Boundaries cannot be overrun.
Arrays are faster for accessing. Strings are slow for accessing.

Array and string are very important data structures. There are many coding interview questions asked based on these two data structures.

The key points of comparison are:

  • Arrays can hold any data type whereas strings can hold only char data.
  • If the string is a char pointer then its size can be changed whereas the size of an array cannot be changed.
  • The last character of the string is a null character but in the array, it is an element of a special type.
  • At the time of declaration, the length of an array is specified in []. In strings, its length is the number of characters plus one null character.
  • Arrays are mutable which means the fields can be modified whereas strings are immutable meaning once an object is created its value cannot be modified.

Arrays and Strings in Different Programming Languages

Array and strings are used differently in different programming languages. For instance, C++ does not have a true array or string type.

Array in C++ programming is just a set of sequential memory locations that can be accessed using pointers or indices. Strings in C++ is a set of 8-bit bytes arranged in sequential memory locations with the last byte set to zero.

There is no provision in C++ to prevent the program from writing outside the boundary of an array and destroying the memory content in this process.

But this is not true in Java. Java has true array types and string types with protective features. This prevents the program from writing outside the memory bounds of an array or a string.

Thus arrays and strings are true objects in Java. A string is not a primitive data type in Java, rather it is a class.

Other related comparisons:

This is all about the difference between array and string in C++ and Java programming. If you have any doubt, let’s discuss in the comment.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *