answersLogoWhite

0


Best Answer

The char type store only one symbol of data (letter, digit, space, tab and so on). The string type is able to keep virtually data of any length. Also string type is based on arrays and uses reference type working with memory.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

A character is used to store a single alphabetic value like 'A' or 'X' etc

A String is used to store a series of alphanumeric values like "Rocky", "#Abcd" etc

[Note: this answer includes a lot of Java-specific examples and info, as the original question was under the Java Programming topic]

More specifically, a Character variable type is designed to hold a single value of the current character set - in Java, this is a 16-bit Unicode character, which defaults to either the local character encoding of the system OR UTF-16 (if the local encoding can't be determined), so ANY value in that Unicode character set (including non-printable ones, and various control characters) can be stored in a 'char' variable. In Java, characters are a primitive character type (like 'int' and 'long'), and are NOT objects, though they have the appropriately named Object class available to them, which is java.lang.Character in this case.

A String is an array (in the conceptual sense) of 0 or more characters; most programming languages have a special "string termination" character which indicates the end of a string variable's value. In Java, a String is an Object of type java.lang.String, and can be filled with any UTF-16 character code. The maximum length of a Java String object is tied to the MAX_INT value, which is 232 in Java. Thus, as String.length() must return a positive integer value, you can store up to (232) - 2 values in a String (plus the character termination character).

You should NEVER assign a character variable directly to a String, vice versa, or anything similar (including assuming integers and characters are interchangeable). Treat them as the completely different types that they are (i.e use methods to move values between them). Even in non-Java languages, making assumptions about compatibility between String and Character values is sloppy programming.

In the JVM, a character variable is stored in a single 16-bit memory allocation, and changes to that Java variable overwrite that same memory location. This makes creating or updating character variables very fast and memory-cheap, but increases the JVM's overhead compared to the static allocation as used in Strings (see below).

The JVM stores Java Strings in a variable size memory space (essentially, an array), which is exactly the same size (plus 1, for the string termination character) of the string when the String object is created or first assigned a value. Thus, an object with initial value "HELP!" would be allocated 96 bits of storage ( 6 characters, each 16-bits in size). This value is considered immutable, allowing the JVM to inline references to that variable, making static string assignments very fast, and very compact, plus very efficient from the JVM's point of view. However, any modification (including clearing the value) of a String Object is expensive - the JVM does NOT modify the memory location originally assigned to the String. Rather, it copies the value somewhere, makes the adjustments required, marks the original memory location for Garbage Collection, then creates a whole new static array to hold the new String value in. Thus, modifying a String variable is very expensive, and NOT recommended in Java.

In Java, Best Practices for coding mean using the String object only for static content - that is, use a String object when you are assuming that it will be the equivalent of a Constant. If you plan to modify the value of a String (other than very, very rarely), you should use the StringBuffer object instead, which is optimized in the JVM for more efficient modification of the value.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A character is a simple scalar data type that can hold a character in the implementation's character set. char mycharacter = 'A'; A string is an array of characters. char mystring[] = 'ABCDEFG'; Note that the allocated size is 8, not 7, because there is a terminating null at the end. You can access individual characters, such as mystring[3] which is 'D', or you can refer to the whole string, such as mystring, but note that is a pointer. One BIG BIG problem with strings is that you must not overrun the end or beginning of the memory allocated for it. In the example case, that is exactly 8 characters.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

String is a sequence of characters.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

string is just an array of characters, other than that not much.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

In Java, a character is a 16-bit Unicode character. In C, a character is an 8-bit ASCII character.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A character or 'char' is used to define a single character only. A string can store any number of characters in succession.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Character data type stores a single character Where as string data type stores a group of characters.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is relationship between string and char data type?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

In java is A string the same thing as a char?

No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.


What type of data is held in a string?

String in C is basically a Character 1-D array, whose last character is a NULL ['\0']. It is declared as follows: char array_name[size]; Ex. char wiki[10];


What is char data types?

The char data types holds a single ASCII (or unicode) value, so it holds any character, for example: '2', 'r', or '~'. The problem is it only holds one character, not a whole string. That is why the string was developed; it holds a whole bunch of characters in a row. But strings cant be compared with < and >, so for alphabetical ordering, use char.


What are the nine basic data types?

Byte Short Int Long Float Double Char Bool String


What is size of char far data type?

char is a primitive data type and depends on the programming language and the operating system.

Related questions

Where string is primitive data type of string data type?

String is not primitive data. Only char,int,double,and boolean are!


In java is A string the same thing as a char?

No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.No.A char is a single Unicode character. It is stored as a primitive (i.e., non-object) data. A string can be considered as an array of chars - Java stores it as an object.


Can you make an integer pointer variable to char data?

Of course. But why? int *p = (int *)"string";


What is primare datatype in c?

primary datatypes means the data types which are provided by developer of language himself like int,float,double,char are the primary data types in c language where as the String,array are nothing but the derived data types. for Ex.we derived the String data type from char datatype using array system.


What type of data is held in a string?

String in C is basically a Character 1-D array, whose last character is a NULL ['\0']. It is declared as follows: char array_name[size]; Ex. char wiki[10];


What is char data types?

The char data types holds a single ASCII (or unicode) value, so it holds any character, for example: '2', 'r', or '~'. The problem is it only holds one character, not a whole string. That is why the string was developed; it holds a whole bunch of characters in a row. But strings cant be compared with < and >, so for alphabetical ordering, use char.


What are the nine basic data types?

Byte Short Int Long Float Double Char Bool String


What are Strings in C programming?

Arrays of chars are strings. there is a built in librray, that handles string string.h but the data-type is held as arrays of chars. char[10] c="string"; translate to ['s','t','r','i','n','g',\0] Arrays of chars are strings. there is a built in librray, that handles string string.h but the data-type is held as arrays of chars. char[10] c="string"; translate to ['s','t','r','i','n','g',\0]


What is the syntax for string in c?

The C programming language has no notion of a string. The C runtime libraries interpret a string as an array of 'char' (sometimes 'unsigned char'), where a byte (char) with numerical value zero (often written as '\0' in C) denotes the end of the string. Modern variations also support modern forms of strings based on different data types (wchar, etc) in order to support more complex encodings such as Unicode. These, too, are interpretations of combinations of language features, but not a built-in part of the language.


What is the difference between string and class?

A string is a specific class that is used for dealing with text data


What is the c plus plus command to read a line?

You can use cin which located in iostream.h You have to use certain data type to read string, for instance, array of char


What is the relationship between two data sets when one data set of data values increases while the other decreases?

There is an inverse relationship between the datasets.