Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How many bytee in memory string occupies?

807580Sep 8 2009 — edited Sep 13 2009
Hi,

Back to basics. If I define a string say
String str = "0x23FF";
actually how many bytes it takes in the memory ? It says that each char in java is 2 bytes and hence 12 bytes.
but a code like
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.

I was thinking which would be the efficient way to store a string of this sort (hex numbers) as Long or String ?

Regards,
HiAll
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 11 2009
Added on Sep 8 2009
9 comments
571 views