What does function
sizeof() gives us the size of used memory in bytes why there is difference between “a” and ‘a’ ?
"a" is a
'a' is a
Hence there representation in memory is different.
I’m not a string/char expert so I can’t tell you much about the actual sizes of these objects, I’m afraid. However, you might want to take a look at
See the help, eg
This is extensively documented, please read this chapter:
Are you sure that there is an explanation why computer needs 1 byte to store
String "a" while to store
char 'a' it needs 4 byte?
Yes. Please just read it, and feel free to ask here if something is unclear.
Also note that
sizeof("a") does not tell how how much memory the computer uses (since it does not contain the information for length, just the contents).
You are possibly looking for
Base.summarysize("a"), which is 8 bytes for the length (
Int) plus the byte
0x61 for the character ‘a’ (Strings are stored in UTF-8, which is variable-width).