Is char and string the same?

On technical groud, we can say that both a character array and string contain the sequence of characters and used as a collection of characters. But there are significant differences between both which we would discuss below.

The following are the important differences between String and Character array.

Sr. No.KeyStringCharacter array1ImplementationString is implemented to store sequence of characters and to be represented as a single data type and single entity.Character Array on the other hand is a sequential collection of data type char where each element is a separate entity.2Internal implementationString internal implementation makes it immutable in nature.Character array is mutable in nature on the other hands.3Built-in FunctionsAs String is a class so is provided with various built-in function substring(), charAt() etc.No built in functions are provided in Java for operations on Character Arrays.4ConcatenationString can be concatenated either by using + operator or using its built in function concate().Character array could not use either of these function/operator to get concatenated.5StorageStrings could be get stored in any random order in the part of memory which is known as SCP (String Constant Pool).Elements in Character Array are stored contiguously in increasing memory locations which is known as Heap.6ConversionA String can be converted into Character Array by using the toCharArray() method of String class.On the other hand, a Character Array can be converted into String by passing it into a String Constructor.

Example of String vs Character Array

 Live Demo

JavaTester.java

public class JavaTester{
   public static void main(String[] args) {
      String s = "HELLO";
      char [] ch = s.toCharArray();
      char[] a = {'H', 'E', 'L', 'L', 'O'};
      String A = new String(a);
      System.out.println(s);
      System.out.println(A);
   }
}

Output

HELLO
HELLO

Is char and string the same?


Is char and string the same?

But the fundamental difference between character array and string is that the “character array” can not be operated with standard operators, whereas, the “string “objects can be operated with standard operators. Let’s study the other differences between a character array and string.

Content: Character Array Vs String

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for ComparisonCharacter ArrayStringBasicCharacter array is collection of variables, of character data type.String is class and variables of string are the object of class "string".Syntaxchar array_name [size];string string_name;IndexingAn individual character in a character array can be accessed by its index in array.In string the particular character can be accessed by the function "string_name.charAt(index)".Data TypeA character array does not define a datatype.A string defines a datatype in C++.OperatorsOperators in C++ can not be applied on character array.You may apply standard C++ operator on the string.BoundaryArray boundaries are easily overrun.Boundaries will not overrun.AccessFast accessing.Slow accessing.

Definition of Character Array

A character array is a collection of the variables of “char” datatype; it can be a one-dimensional array or a two-dimensional array. It is also called “null terminated string”. A Character array is a sequence of the characters that are stored in consecutive memory addresses. In a character array, a particular character can be accessed by its index. A “Null character” terminates the character array”.

Is char and string the same?

Let’s take an example of character array:-

char name[ ]={'A', 'j', 'a', 'y', '\0'};
or
char name[ ]="Ajay";

Here, “char” is a character data type, “name” is a variable name of the character array. I had shown two ways to initialize the character array. In the first method, the null is explicitly mentioned and in the second method, the compiler automatically inserts the null.

The end of the string is always a null character; it is the terminating character of the character array. A character array is not a built-in data type; we create character array by declaring it. You can not apply standard operators on a character array.

To operate on character array there are some built-in function such as,(strlen( ), strlwr( ), strupr( ), strcat( )). As the standard operators can not be applied to a character array, they cannot take part in any expression.

The character pointer to a character array can also be created.

Let’s understand it with an example.

char s1[ ]="Hello";
char s2[ ]="Sir";
s1= s1+s2; //error operators can not be applied
s2=s1; //error

Character pointer

char *s= "Morning";
char *p;
p=s; //executes

In above example, we had declared two character array s1, s2 and two character pointers s and p. Character array s1 and s2 are initialized, we can see that neither the addition operator(+) nor the assignment operator works on the character array. But a character pointer can be assigned to another character pointer.

Remember once the character array is initialized it can not be initialized again to another set of character. The access to a character array or a null terminated string is fast as compared to string in C++.

Definition of String

A string is not a built-in data type of C++. It a class object of type “string”. As in C++ creating a class is just like creating a “type”. The class “string” is a part of C++ library. It holds the set of character or character array as a whole. There are three reasons behind the development of a standard string class.

  • First is “consistency”, the character arrays are not data types in their own right.
  • Second is “convenience”, you can not use standard operators on a character array.
  • Third is “safety”, array boundaries are easily overrun.

Is char and string the same?

Let us understand strings with an example.

string s1;
s1= "Hello";
string s2("Good morning");
string s3="Hennery";
string s4;

In the above declaration, four string variable or objects (s1, s2, s3, s4) are declared.  In above declaration, I had shown three ways of initializing the string. The string s1 is declared and then separately initialized. The string s2 is initialized by the constructor of class “String”. The string s3 is initialized at the time of its declaration as normal data type do. We can apply the standard operator to the string variables.

s4=s1;// assigning one string object to other
s4=s1+s2; // adding two string and storing the result in third string
if(s3 > s2) // comparing two strings
strings s5(s1); creating a new string object using existing string object

In above code, various operators are applied on a string and various operations are performed. The first statement copies one string object to another string object. In the second statement, two string are concatenated and stored in a third string. In the third statement, two string are compared. In the fourth statement, a new string object is created using the already existing string object.

Is string and char in C same?

The C programming language distinguishes character constants from string constants by using quotation marks in the following manner: 'c' is the character c, while "c" is a string of length 1 consisting of the single character c.

Is string made up of char?

Strings are often made up of characters. They are useful for storing human-readable data, like sentences, or lists of alphabetical data, like the nucleic acid sequences of DNA.