How many bytes in memory a string occupy in Java language?
3 posters
Page 1 of 1
How many bytes in memory a string occupy in Java language?
I am reposting this question.
How many bytes in memory a string occupy in Java language?
Suppose i write:
String name="Bill Gates";
So, how much memory it requires? Whether it can store a UNICODE character in the string. How internally it is implemented, using a 1 byte char array or 2 byte unicode char array? I read somewhere that Java's char data type is 16 bit wide.
Thanks!!!
How many bytes in memory a string occupy in Java language?
Suppose i write:
String name="Bill Gates";
So, how much memory it requires? Whether it can store a UNICODE character in the string. How internally it is implemented, using a 1 byte char array or 2 byte unicode char array? I read somewhere that Java's char data type is 16 bit wide.
Thanks!!!
qinazaza- Newbie
- Number of posts : 6
Warning :
Reputation :
Registration date : 2009-03-20
Re: How many bytes in memory a string occupy in Java language?
I think that strings take up two bytes, but I may be thinking of an integer.
Re:How many bytes in memory a string occupy in Java language?
byte[] utf8 = null;
int byteCount = 0;
try {
utf8 = str.getBytes("UTF-8");
byteCount = utf8.length;
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.printf("UTF-8 Byte Count: %d\n", byteCount);
prints only 6.
length is not how many bytes the array occupies. It's how many elements the array has.
It's a byte array, so subject to object overhead, storage for the array length and alignment, it will be a number of bytes. In this case, it's the number of bytes in the UTF-8 encoding of the data in the string.
Since Strings don't use UTF-8 but UTF-16 the number of bytes it takes to encode a string in UTF-8 won't correspond to the number of bytes the String object takes in any way.
Bariatric Surgery
steroids
int byteCount = 0;
try {
utf8 = str.getBytes("UTF-8");
byteCount = utf8.length;
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.printf("UTF-8 Byte Count: %d\n", byteCount);
prints only 6.
length is not how many bytes the array occupies. It's how many elements the array has.
It's a byte array, so subject to object overhead, storage for the array length and alignment, it will be a number of bytes. In this case, it's the number of bytes in the UTF-8 encoding of the data in the string.
Since Strings don't use UTF-8 but UTF-16 the number of bytes it takes to encode a string in UTF-8 won't correspond to the number of bytes the String object takes in any way.
Bariatric Surgery
steroids
bujjiammulu- Newbie
- Number of posts : 82
Warning :
Reputation :
Registration date : 2010-05-11
Similar topics
» String Compression
» (Java) Network Based Screen Share
» Hardest Web Development Language
» Sacred Fire [ Language Arts Story ]
» Can the code of a Game Maker game be converted to another programming language ?
» (Java) Network Based Screen Share
» Hardest Web Development Language
» Sacred Fire [ Language Arts Story ]
» Can the code of a Game Maker game be converted to another programming language ?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum