answersLogoWhite

0


Best Answer

A char is a single character. A String is a collection of characters. It may be empty (zero characters), have one character, two character, or many characters - even a fairly long text.

The single quote (') is used to deliniate a character during assignment:

char someChar = 'a';

The double quote (") is used to delineate a string during assignment:

String someString = new String("hello there");

Note that char is a primitive data type in Java, while String is an Object. You CANNOT directly assign a char to a String (or vice versa). There is, however, a Character object that wraps the char primitive type, and Java allows method calls to be made on the char primitive (automagically converting the char to Character before doing so).

i.e. these ALL FAIL:

someString = SomeChar;

someString = new String(someChar);

However, these WILL work:

someString = Character.toString(someChar);

someString = someChar.toString();

Also note that a String is a static memory allocation, while a character's is dynamic. By that, I mean that when a String is created, it is allocated a memory location exactly big enough to fit the assigned string. Any change to that String forces and entirely new memory location to be allocated, the contents of the old String copied in (with the appropriate changes), and the old String object subject to garbage collection. Thus, making changes to a String object are quite inefficient (if you want that kind of behaviour, use StringBuffer instead).

A character is allocated but once; all subsequent changes to a character variable simply overwrite that same memory location, so frequent changes to a character variable incur no real penalty.

User Avatar

Wiki User

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

Wiki User

13y ago

The 'string' (more correctly 'char') type in C and C++ is a single scalar variable that can hold one character in the host's character set. The c-type string is an array of 'char' types. In this context, the array is of fixed size, and you need to provide code to manipulate it.

In C++, you can have classes, and you can have a 'string' class, or type. In this context, you can have automatic allocation/deallocation, expansion/contraction, and manipulation provided by the class.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Strings "like this" are constant character-array literals, so they can be used as 'char *' or 'char []' values, but they cannot be modified.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How does a 'string' type string differ from a c-type string?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Difference between ctype and directcast in vbnet?

The difference between the two keywords is that CTypesucceeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType.


What are the different issues if a string is implemented as a primitive or as a character array?

Even in languages where string is a type, it is still an array of characters. A computer can not represent a string as a primitive type. That said, the difference between using the programming language string object or an array or list if characters will differ based on language. Though in most languages which offer a string type, it would mean that you'd have to implement all the functions provided for you by the type on your own. A string object class simply provides a series of utility functions to manipulate a character array within.


What is the difference between string and char array?

3 differences.................. 1. length wise.... 2.initialization 3. null terminated length of char array is differ from string........ initialization of string is differ from char....... and string is null terminated...........


Where string is primitive data type of string data type?

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


Can you store non primitive data type in the array list?

Yes you can store non primitive data type variables in an array. String is a non primitive data type. You can declare a string array as: String a[]=new String[10];


What is the best type of yoyo string?

Type 8 cotton string works good, and it can be colored to if you want.


What is in string?

A string is a collection of words or characters in '' or "" it is also a data type.


What is a collective noun for string?

The collective noun for 'string' is a ball of string.The noun 'string' is a standard collective noun for:a string of ducksa string of horsesa string of mulesa string of pearlsa string of poniesa string of racehorsesa string of violinists


Is string is primitive or user defined data type?

String - is primitive data typestring - is user defined data type


What is string in Python?

A string is a collection of words or characters in '' or "" it is also a data type.


What is the difference between a 1886 type 1 and type 2 Indian head penny?

Here's the description given by the experts at CoinFacts.com:Type 1 - last feather on the Indian's headdress points between the I and the CType 2 - last feather on the Indian's headdress points between the C and the A


What is return type of string in c?

In C programming, a string doesn't have a specific return type as it's essentially an array of characters. So, if a function is returning a string, it should be declared to return a pointer to a char (char*), since a string in C is represented as an array of characters terminated by a null character ('\0').