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.
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>];
Example:
arr[]={‘a’,'b’,'c',’d'}
The above is an example of a character array. Just like characters, you can also use arrays to store numeric values.
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.
Declaration:
string string_name;
Example:
arr[]={‘hello’, ‘good’ ,‘day’, ‘please’}
The above is an example of the string array.
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.
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.
Very informative Post. I was asked this question in one of the technical interviews.