Central Games Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

How many bytes in memory a string occupy in Java language?

3 posters

Go down

How many bytes in memory a string occupy in Java language? Empty How many bytes in memory a string occupy in Java language?

Post by qinazaza Sat Mar 21, 2009 6:12 am

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!!!

qinazaza
Newbie
Newbie

Number of posts : 6
Warning :
How many bytes in memory a string occupy in Java language? Left_bar_bleue0 / 1000 / 100How many bytes in memory a string occupy in Java language? Right_bar_bleue

Reputation :
How many bytes in memory a string occupy in Java language? Left_bar_bleue0 / 1000 / 100How many bytes in memory a string occupy in Java language? Right_bar_bleue

Registration date : 2009-03-20

Back to top Go down

How many bytes in memory a string occupy in Java language? Empty Re: How many bytes in memory a string occupy in Java language?

Post by Luke(ADMIN) Sat Mar 21, 2009 10:20 am

I think that strings take up two bytes, but I may be thinking of an integer.
Luke(ADMIN)
Luke(ADMIN)
Admin
Admin

Male
Number of posts : 838
Age : 30
Location : Right Behind You
Job/hobbies : Hello! I am a programmer/hacker. I play piano and French Horn. I love mountain biking and kayaking.
Warning :
How many bytes in memory a string occupy in Java language? Left_bar_bleue0 / 1000 / 100How many bytes in memory a string occupy in Java language? Right_bar_bleue

Reputation :
How many bytes in memory a string occupy in Java language? Left_bar_bleue999 / 100999 / 100How many bytes in memory a string occupy in Java language? Right_bar_bleue

Registration date : 2008-07-17

http://www.lujosoftware.com

Back to top Go down

How many bytes in memory a string occupy in Java language? Empty Re:How many bytes in memory a string occupy in Java language?

Post by bujjiammulu Tue May 11, 2010 11:50 am

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

bujjiammulu
Newbie
Newbie

Number of posts : 82
Warning :
How many bytes in memory a string occupy in Java language? Left_bar_bleue0 / 1000 / 100How many bytes in memory a string occupy in Java language? Right_bar_bleue

Reputation :
How many bytes in memory a string occupy in Java language? Left_bar_bleue0 / 1000 / 100How many bytes in memory a string occupy in Java language? Right_bar_bleue

Registration date : 2010-05-11

Back to top Go down

How many bytes in memory a string occupy in Java language? Empty Re: How many bytes in memory a string occupy in Java language?

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum