When we use the char datatype instead of string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When we use the char datatype instead of string?

Why could be better choice a char than a string with only one character?

19th Nov 2016, 2:16 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
5 Answers
+ 7
I recommend to use char when you want to store single letters only. Since you do not want to accidentally add in more letters into a variable. Secondly, char will make the system go faster compared to string if your system is consuming hundreds of megabytes per second.
19th Nov 2016, 2:54 PM
Wen Qin
Wen Qin - avatar
+ 5
Size of char is 1 byte whereas string is 4 bytes. Use string when you need to store large data where you don't know what will be the input data length. Use char when you know length of data and this will more faster than string, use less memory
19th Nov 2016, 2:58 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 2
It takes less memory to do 'a' as a character rather than "a" as a string.
19th Nov 2016, 2:23 PM
Ben
Ben - avatar
0
and when you use only a char? for me it seems very limited.
19th Nov 2016, 2:25 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
0
Thanks for explanations!
19th Nov 2016, 3:02 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar